Nothingman0 Posted March 3, 2007 Share Posted March 3, 2007 Hello, everyone. I'm still quite new to php and mysql but I have been unable to figure this out. I'm attempting to display a specific row and column from a mysql database based on the user. $result = mysql_query("SELECT fbleague FROM users WHERE username = $_SESSION[username]"); fbleague is the column users is the table username is the primary key column $_SESSION[username] is the username variable Then later I try to display it with echo "You are in league $result "; I know $_SESSION[username] is set correctly because I can display that with no problems. Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/ Share on other sites More sharing options...
genericnumber1 Posted March 3, 2007 Share Posted March 3, 2007 you need to put quotes around string variables in a mysql query, I also like to put {} around variables inside double quotes... $result = mysql_query("SELECT fbleague FROM users WHERE username = '{$_SESSION['username']}'"); Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198798 Share on other sites More sharing options...
DeathStar Posted March 3, 2007 Share Posted March 3, 2007 why not use it like this: $username = $_SESSION['username']; $result = mysql_query("SELECT fbleague FROM users WHERE username='$username'"); thats how I work I just use $_SESSION['userid']. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198825 Share on other sites More sharing options...
Barand Posted March 3, 2007 Share Posted March 3, 2007 According to the manual you only need the {} when using 2D (or more) array values such as $_SESSION['x']['y'] in a string. However, I agree with GC1 and use them whenever I insert an array element in a string, or avoid the issue competely as deathstar suggests. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198826 Share on other sites More sharing options...
Nothingman0 Posted March 3, 2007 Author Share Posted March 3, 2007 Thanks for the quick responses. That seemed to work, sort of. It didn't used to display anything but now it displays Resource id #6 for the value. The value is actually 0. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198839 Share on other sites More sharing options...
DeathStar Posted March 3, 2007 Share Posted March 3, 2007 try this: if (!$username){ echo "None";} else{ echo "$username";} Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198841 Share on other sites More sharing options...
Nothingman0 Posted March 3, 2007 Author Share Posted March 3, 2007 try this: if (!$username){ echo "None";} else{ echo "$username";} Maybe I'm not understanding what you want me to check with your code suggestion but; echo "Welcome, <b>$_SESSION[username]</b>.; does return the username correctly. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198846 Share on other sites More sharing options...
Barand Posted March 3, 2007 Share Posted March 3, 2007 $result is a result set and not the selected value, you need to extract that from $result. With a single value as you are selecting the easiest way is $fbleague = mysql_result ($result, 0, 0); which gets the first field of the first row. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198847 Share on other sites More sharing options...
Nothingman0 Posted March 3, 2007 Author Share Posted March 3, 2007 That worked! Thanks a million, Barand and everyone. Link to comment https://forums.phpfreaks.com/topic/41048-retrieving-database-info-based-on-user/#findComment-198848 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.