Jump to content

Display data from mysql in php

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi,

fairly new to coding, as you will soon see, and can't get the data to display from an SQL DB. Can anyone offer any help as to why its not displaying?

I've replaced the DB access details, because obviously, everyone will want to hack my test site ;P


<?php
$dbhost = 'host';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
$dbname = 'test1';
mysql_select_db($dbname);
echo "connected sucessfully";
?>


<?php
$query="SELECT * FROM test1";
$result=mysql_query($query);

#$num=mysql_numrows($result);



echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {
$fname=mysql_result($result,$i,"fname");
#$sname=mysql_result($result,$i,"sname");
#$address=mysql_result($result,$i,"address");
#$city=mysql_result($result,$i,"city");
#$county=mysql_result($result,$i,"county");
#$postcode=mysql_result($result,$i,"postcode");
#$hphone=mysql_result($result,$i,"hphone");
#$mphone=mysql_result($result,$i,"mphone");
#$email=mysql_result($result,$i,"email");
#$bmanufacturer=mysql_result($result,$i,"bmanufacturer");
#$bmodel=mysql_result($result,$i,"bmodel");
#$bcolor=mysql_result($result,$i,"bcolor");
#$bserialnumber=mysql_result($result,$i,"bserialnumber");
#$ID=mysql_result($result,$i,"ID");
echo $fname;
#echo $fname $sname $address $city $county $postcode $hphone $mphone $email $bmanufacturer $bmodel $bcolor $bserialnumber $ID;
$i++;
}

mysql_close();

?>
Really appreciate any help.

PS, am aware of the commented majority of what should be being retrieved.

#2
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
any takers?

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
You should read up on arrays and DB access a bit more in PHP.

example :
<?php
	$q = mysql_query("SELECT * FROM test1 ORDER BY ID ASC") or die(mysql_error());
	while($result = mysql_fetch_assoc($q))
	{
		echo $result['fname'] . "<br />" . $result['sname'];
	}
?>

Also, what error messages do you receive when running your script?

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#4
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
Thanks, will try that.

DEViANT said:

Also, what error messages do you receive when running your script?

No error message, just no DB content displayed.

#5
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
the error message now is "No database selected", after I added the code you suggested.

#6
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
I notice that the table name you are 'selecting' from in your SQL query is the same as the database you connect to, is it set up like that in your SQL Database as well?

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#7
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
yeah, both the db and the table are called test1, do you think I should change one, to avoid any potential conflict in the script?

#8
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
please try running the following code. If it fails, try renaming the table name and remember to make the necessary changes in the script :
<?php

	mysql_connect('host','username','password') or die(mysql_error());
	mysql_select_db('database') or die(mysql_error());

	$q = mysql_query("SELECT * FROM test1 ORDER BY ID ASC") or die(mysql_error());
	while($result = mysql_fetch_assoc($q))
	{
		echo $result['fname'] . "<br />" . $result['sname'];
	}
	
?>

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#9
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
I've figured out the problem.

The hosting site actually adds its own prefix to the DB name when I created it, so it was freei_blah_blah_test1.

very embarrassing.

on the plus side, the code you provided works really well.

Thanks a lot for your help anyway, DEViANT

#10
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Your very welcome. Happy Coding :)

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it