eqnastun Posted October 25, 2008 Share Posted October 25, 2008 Hello, I am trying to create a small piece of code that will load as a php page that can display 2 columns from a table. The columns are in the middle of the table but I am looking to output name and e-mail into a list. Here is what I have so far but I am not getting any results. <html> <head> <title>PHP Test</title> </head> <body> <?php echo "<b><center>Database Output</center></b><br><br>"; $username="root"; $password="xxxx"; $database="test2"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM fr_users"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $field1-name=mysql_result($result,$i,"login"); $field2-name=mysql_result($result,$i,"alias"); echo "<b>$field1-name - $field2-name2</b><hr><br>"; $i++; } ?> </body> </html> Appreciate any help. Thank You. I am sure this is very easy to do. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 25, 2008 Share Posted October 25, 2008 remove mysql_close(); are you getting any errors? EDIT: change it all to this... <html> <head> <title>PHP Test</title> </head> <body> <?php echo "<b><center>Database Output</center></b><br><br>"; $username="root"; $password="xxxx"; $database="test2"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM fr_users"; $result=mysql_query($query); $result = mysql_fetch_array($result); echo "<b>".$result['login']." - ".$result['alias']."</b><hr><br>"; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
php new bie Posted October 25, 2008 Share Posted October 25, 2008 I edit some of your code , This one below list two columns in a table with format Data1:data2 <html> <head> <title>PHP Test</title> </head> <body> <?php echo "<b><center>Database Output</center></b><br><br>"; $username="root"; $password="xxxx"; $database="test2"; mysql_connect('localhost',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM fr_users"; $result=mysql_query($query); //$num=mysql_numrows($result); mysql_close(); //$i=0; while ($row = mysql_fetch_array($result)) { //$field1-name=mysql_result($result,$i,"login"); //$field2-name=mysql_result($result,$i,"alias"); echo $row['login'] . ":" . $row['alias']."<br />"; // desired columns names } ?> </body> </html> Appreciate any help. Thank You. I am sure this is very easy to do. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 25, 2008 Share Posted October 25, 2008 yeah, but you don't need a while() loop. $row = mysql_fetch_array($result); will do the exact same thing, and you need to get rid of mysql_close(); all mysql after that is being denied. Quote Link to comment Share on other sites More sharing options...
php new bie Posted October 25, 2008 Share Posted October 25, 2008 yeah, but you don't need a while() loop. $row = mysql_fetch_array($result); will do the exact same thing, and you need to get rid of mysql_close(); all mysql after that is being denied. i test the code and find this error : Notice: Use of undefined constant localhost - , so you need to add single quote 'localhost' and without while loop it just show the first record with desired columns in the selected TABLE and neglect the rest ! -php new bie Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 25, 2008 Share Posted October 25, 2008 and without while loop it just show the first record with desired columns in the selected TABLE and neglect the rest ! no it won't. It would only do the same result if the mysql query had LIMIT 1 in it. But it doesn't you don't need a loop. setting $row to fetch array will place all results into the $row array without the need for a loop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.