wright67uk Posted April 8, 2011 Share Posted April 8, 2011 Why would the code below display all of the results from my sql query opposed to just the first field? ie. echo $field1 <?php mysql_connect(###"); mysql_select_db("###") or die("Unable to select database"); $result = mysql_query("SELECT name FROM business WHERE type ='Restaurant' ORDER BY name"); $numrows = mysql_num_rows($result); for($x=0;$x<$numrows;$x++){ $result_row = mysql_fetch_row($result); $field1 = $result_row[0]; $field2 = $result_row[1]; $field3 = $result_row[2]; echo $field1; } ?> many thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/ Share on other sites More sharing options...
MatthewJ Posted April 8, 2011 Share Posted April 8, 2011 mysql_fetch_row... I think you mean mysql_fetch_array()? Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1198992 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 mysql_fetch_row is fine, since he's only using numeric keys. You're only SELECTing one field, so $result_row[1] and $result_row[2] are blank, but I don't really know what you mean. It's echoing the `name` field from all the rows in your table. What do you want it to do? If you want to show only the first, say, 10 rows you can use "LIMIT 0,10" at the end of the query. PS: may I suggest a cleaner loop? No $numrows necessary. while($result_row = mysql_fetch_row($result)) { $field1 = $result_row[0]; $field2 = $result_row[1]; $field3 = $result_row[2]; echo $field1; } Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1198993 Share on other sites More sharing options...
wright67uk Posted April 9, 2011 Author Share Posted April 9, 2011 Thanks for the replys, here is what I am trying to achieve; Say for example SELECT name gave me two results; nameone and nametwo I would want; $field1 = nameone $field2 = nametwo where as at the moment im getting; $field1 = nameone nametwo Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199147 Share on other sites More sharing options...
3raser Posted April 9, 2011 Share Posted April 9, 2011 Could this help? <?php mysql_connect(###"); mysql_select_db("###") or die("Unable to select database"); $result = mysql_query("SELECT name FROM business WHERE type ='Restaurant' ORDER BY name"); $numrows = mysql_num_rows($result); $result_row = mysql_fetch_assoc($numrows); if(!$i) $i = 0; foreach($result_row as $value) { $field[$i] = $result_row['name']; } //display for($x = 0; $x < $i; $x++) { echo "Result ". $i ." from the database equals ". $field[$i] . "<br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199151 Share on other sites More sharing options...
wright67uk Posted April 9, 2011 Author Share Posted April 9, 2011 Hi, i got the below code to work in the end; $query = mysql_query("SELECT name FROM business WHERE type ='Restaurant' ORDER BY name"); echo mysql_error(); while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0]; echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>"; where ive put echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>"; is it possible for php to count how many results are to be output and then provide the right amount of variables? for example... for four results php would display; echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>$nt[3]"; but if i only had 2 results then; echo "$nt[0]<br>$nt[1]"; Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199158 Share on other sites More sharing options...
3raser Posted April 9, 2011 Share Posted April 9, 2011 Hi, i got the below code to work in the end; $query = mysql_query("SELECT name FROM business WHERE type ='Restaurant' ORDER BY name"); echo mysql_error(); while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0]; echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>"; where ive put echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>"; is it possible for php to count how many results are to be output and then provide the right amount of variables? for example... for four results php would display; echo "$nt[0]<br>$nt[1]<br>$nt[2]<br>$nt[3]"; but if i only had 2 results then; echo "$nt[0]<br>$nt[1]"; Yes: $i = -1; foreach($nt as $value) { $i++; echo $nt[$i]."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199170 Share on other sites More sharing options...
MatthewJ Posted April 11, 2011 Share Posted April 11, 2011 mysql_fetch_row is fine, since he's only using numeric keys. You're only SELECTing one field, so $result_row[1] and $result_row[2] are blank, but I don't really know what you mean. It's echoing the `name` field from all the rows in your table. What do you want it to do? If you want to show only the first, say, 10 rows you can use "LIMIT 0,10" at the end of the query. PS: may I suggest a cleaner loop? No $numrows necessary. while($result_row = mysql_fetch_row($result)) { $field1 = $result_row[0]; $field2 = $result_row[1]; $field3 = $result_row[2]; echo $field1; } Um... no, he was stating that the whole row was printing when he only wanted the first fields data. Exactly why I suggested mysql_fetch_array as it returns an array accessible by numeric key. Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1200058 Share on other sites More sharing options...
Pikachu2000 Posted April 11, 2011 Share Posted April 11, 2011 Exactly why I suggested mysql_fetch_array as it returns an array accessible by numeric key. mysql_fetch_array() returns both an enumerated and an associative array. mysql_fetch_row() returns only an enumerated array. mysql_fetch_assoc() returns only an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1200080 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.