Merriick Posted July 1, 2010 Share Posted July 1, 2010 Im using a online beginners tutorial to retrieve multiple rows from the database, thats not happening Instead its returning one row and it appears to be blank. No doubt theres some holes but stuffed if i know what anyway Code $link = mysql_connect("$dbServer1", "$dbUser1", "$dbPass1") or die("Could not connect post"); print "Connected successfully to $dbServer1 Server<br>"; mysql_select_db("$dbName1") or die("Could not select database your_db_name1"); print "Database $dbName1 selected successfully<br><br><br>"; $query=("SELECT * FROM phpc_uid"); $result=mysql_query($query); $num=mysql_numrows($result); print "Number of rows equals $num"; $i=0; while ($i < $num) { $subject=mysql_result($result,$i,"subject"); $Description=mysql_result($result,$i,"description"); print "$Subject and $Description"; $i++; } Page is returning this Connected successfully to Calendar Database calendar selected successfully Number of rows equals 1 Warning: mysql_result() [function.mysql-result]: subject not found in MySQL result index 4 in C:\wamp\www\Calendar\testmysql.php on line 43 Warning: mysql_result() [function.mysql-result]: description not found in MySQL result index 4 in C:\wamp\www\Calendar\testmysql.php on line 44 Its connecting after that, im guessing doing nothing Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2010 Share Posted July 1, 2010 Well, off hand the only thing that comes to mind is that the result may have different field names then the ones you are trying to access. For example, the case of the letters is important ("subject" != "SUBJECT"). But, I would also suggest some changes to that logic. 1. $query=("SELECT * FROM phpc_uid"); Typically, you should refrain from using "*" as your select criteria unless you need all the data because it uses up additional resources and can be a performance issue on high-traffic sites. Plus, depending on how the data is used it could cause a security problem if fields are added in the future. Besides in this example you are only using two fields, so you should only query for those two. 2. while ($i < $num) { It really makes no sense to define the number of rows returned and then use a counter to get those rows. Just use a while loop that extracts each row. 3. The select query has no error handling, so that could be failing. Example: <?php $link = mysql_connect("$dbServer1", "$dbUser1", "$dbPass1") or die("Could not connect post"); echo "Connected successfully to $dbServer1 Server<br>\n"; mysql_select_db("$dbName1") or die("Could not select database your_db_name1"); echo "Database $dbName1 selected successfully<br><br><br>\n"; $query = "SELECT subject, description FROM phpc_uid"; //$query = "SELECT * FROM phpc_uid"; //DEBUG LINE $result=mysql_query($query); //$firstrow = mysql_fetch_assoc($result); //DEBUG LINE //print_r($firstrow); //DEBUG LINE //exit(); //DEBUG LINE if(!$result) { //Error running query echo "The query failed.<br /><br />\n"; echo "Query: {$query}<br /><br />\n"; echo "Error: " . mysql_error(); } elseif(mysql_numrows($result)<1) { //No results from query echo "No results returned\n"; } else { //Results were returned, show results $num_rows = mysql_numrows($result); echo "Number of rows returned: {$num_rows}<br /><br />\n"; while ($row = mysql_fetch_assoc($result)) { echo "{$row['subject']} and {$row['description']}<br />\n"; } } If you don't get the results you expect, uncomment the debug lines to validate what is returned/available in that table. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2010 Share Posted July 1, 2010 I noticed I had a typo in the two places that I used mysql_num_rows(). You shuld correct that if you are going to use that code. Quote Link to comment Share on other sites More sharing options...
Merriick Posted July 2, 2010 Author Share Posted July 2, 2010 Alrighty then... cheers ill give it a shot Quote Link to comment Share on other sites More sharing options...
Merriick Posted July 6, 2010 Author Share Posted July 6, 2010 Ah HA! Well that worked with no errors Thanks! 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.