ryanfilard Posted September 27, 2012 Share Posted September 27, 2012 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? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/ Share on other sites More sharing options...
KevinM1 Posted September 27, 2012 Share Posted September 27, 2012 (edited) 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. Edited September 27, 2012 by KevinM1 Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/#findComment-1381242 Share on other sites More sharing options...
ryanfilard Posted September 27, 2012 Author Share Posted September 27, 2012 User is text, friend is text, and accepted is one 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); Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/#findComment-1381243 Share on other sites More sharing options...
ryanfilard Posted September 27, 2012 Author Share Posted September 27, 2012 (edited) I made a mistake when retyping the script, it should be this: echo $row_usersfriends['user']; Edited September 27, 2012 by ryanfilard Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/#findComment-1381244 Share on other sites More sharing options...
DavidAM Posted September 27, 2012 Share Posted September 27, 2012 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/#findComment-1381248 Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2012 Share Posted September 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268841-phpsql-script-will-not-display-first-result/#findComment-1381249 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.