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
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
Share on other sites

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

Link to comment
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.