Jump to content

$field1 = $result_row[0]


wright67uk

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1198993
Share on other sites

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/>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199151
Share on other sites

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]";

 

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199158
Share on other sites

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/>";
}

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1199170
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1200058
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/233138-field1-result_row0/#findComment-1200080
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.