Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with printing query result 1

Status
Not open for further replies.

irisha2

Technical User
Feb 19, 2002
9
US
Hello everybody,

I'm trying to print out the mysql query result but I could manage to get only 1 row of data from the table. What am I doing wrong?
Please help me!
I'm just a beginner in Mysql or PHP and this is my first script.

$query = "select lname, fname from main" or die("Invalid query");

$result = mysql_query($query,$db);

$user_result = mysql_fetch_array($result);

for ($i = 0; $i<mysql_fetch_row($result); $i++) {
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}

I'm getting the following output:

Name: Jack, Fisher

But there are more than 1 row in a table.

I would greatly appreciate any help with this.

Thanks,

Irina
 
You are setting $user_result equal to the values of the first row of the dataset but never updating it after that.

Change the lines:
$user_result = mysql_fetch_array($result);
for ($i = 0; $i<mysql_fetch_row($result); $i++) {
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}


to be
for ($i = 0; $i<mysql_fetch_row($result); $i++) {
$user_result = mysql_fetch_array($result);
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}

 
Hi Jim,

Thank you very much for your help. It works!

There is also another way do to the same thing:

while ($user_result = mysql_fetch_array($result)) {
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;].&quot;<br\n>&quot;;
}

 
Yeah, I have found, and still am finding, that with PHP there are usually 2 and sometimes 3 different ways to do the same thing.

I tried to keep the same code that you had just to keep it easy.
 
it's wrong code, you only read half of the table, every other line and not all of them.

change this:
$query = &quot;select lname, fname from main&quot; or die(&quot;Invalid query&quot;);

$result = mysql_query($query,$db);

$user_result = mysql_fetch_array($result);

for ($i = 0; $i<mysql_fetch_row($result); $i++) {
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}


to this:
$query = &quot;select lname, fname from main&quot; or die(&quot;Invalid query&quot;);

$result = mysql_query($query,$db);

while ($user_result=mysql_fetch_array($result)) {
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}

This way you read ALL the lines of the table correctly.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Why do you think the for loop only reads every other record instead of each record?

The counte is incrementng by one so it is grabbing each record as it loops.
 
doing:
for ($i = 0; $i<mysql_fetch_row($result); $i++) {
$user_result = mysql_fetch_array($result);
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}

mysql_fetch_row reads one line and mysql_fetch_array reads another.

When you reach the echo, you have read 2 records instead of one.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
In that code example, you could also just change mysql_fetch_row in the for... loop to mysql_num_rows.

for ($i = 0; $i<mysql_num_rows($result); $i++) {
$user_result = mysql_fetch_array($result);
echo &quot;Name: &quot;.$user_result[&quot;fname&quot;]. &quot;,&quot;.$user_result[&quot;lname&quot;];
}

Then you're not returning a row from $result, just referring to the number of rows in $result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top