Jump to content

Php/sql Script Will Not Display First Result


ryanfilard

Recommended Posts

EDIT: Two things:

 

1. You're using a single-quoted string to output the value. That won't work as only double-quoted strings extrapolate values.

2. You can't output an array value in a string unless you either concatenate or surround it in braces.

 

In short you want either:

 

echo $row_userfriends['user'];

 

or:

 

echo "{$row_userfriends['user']}";

 

If that doesn't solve it, show your query and table structure.

User is text, friend is text, and accepted is one

 

JlyQy.png

This is what I am using to display the data

mysql_select_db($database_main, $main);
$query_userfriends = "SELECT * FROM friends WHERE `friend` = '$uid' LIMIT 9";
$userfriends = mysql_query($query_userfriends, $main) or die(mysql_error());
$row_userfriends = mysql_fetch_assoc($userfriends);

  On 9/27/2012 at 1:40 AM, ryanfilard said:

I made this script:

 <?PHP
       while($row_userfriends = mysql_fetch_array($userfriends))
 {
 echo '$row_userfriends['user']';
 }
 ?>

problem is it works but does not display the first result. Whats wrong? :-\

  On 9/27/2012 at 1:45 AM, ryanfilard said:

mysql_select_db($database_main, $main);
$query_userfriends = "SELECT * FROM friends WHERE `friend` = '$uid' LIMIT 9";
$userfriends = mysql_query($query_userfriends, $main) or die(mysql_error());
$row_userfriends = mysql_fetch_assoc($userfriends);

 

Instead of showing snippets, you should show all of the code that is pertinent. If your latest code comes before the code in your first post, then it is:

 

mysql_select_db($database_main, $main);
$query_userfriends = "SELECT * FROM friends WHERE `friend` = '$uid' LIMIT 9";
$userfriends = mysql_query($query_userfriends, $main) or die(mysql_error());
$row_userfriends = mysql_fetch_assoc($userfriends);
       while($row_userfriends = mysql_fetch_array($userfriends))
 {
 echo '$row_userfriends['user']';
 }

 

If you look at that, you are fetching the first row just before the while. Since you don't do anything with $row_userfriends before the while statement, you are effectively thowing away the first record. Remove the fetch statement (before the while) and you should be golden.

You have a call to mysql_fetch_assoc() before the while() loop begins. That will move the internal data pointer to the second record, and the first one will never be echoed.

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.