payney Posted December 21, 2007 Share Posted December 21, 2007 Hi all. I cannot understand why this is not working, I will first show you the code: $username = $_SESSION["authenticatedUser"]; echo $username; if (!empty($username)) { require ('db.php'); if (!($connection = @ mysql_pconnect($hostname, $username, $password))) die("Could not connect to database"); if (!mysql_select_db($databaseName, $connection)) showerror(); $query = "SELECT * FROM users WHERE username = $username"; if (!($result = @ mysql_query ($query, $connection))) echo "Query error"; while($row = mysql_fetch_assoc($result)) { echo $row['email']; echo $row['username']; echo $row['password']; } With this I get the following error: payneyQuery error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Now, if I hardcode the username, which is 'payney', it works. Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/ Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 this $query = "SELECT * FROM users WHERE username = $username"; should be $query = "SELECT * FROM users WHERE username = '$username'"; you forgot the quotes Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420389 Share on other sites More sharing options...
payney Posted December 21, 2007 Author Share Posted December 21, 2007 But for some reason its not falling into the while loop thing. while($row = mysql_fetch_assoc($result)) { echo $row['email']; echo $row['username']; echo $row['password']; } Its just ignoring all this? Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420391 Share on other sites More sharing options...
jitesh Posted December 21, 2007 Share Posted December 21, 2007 Check by this : if (!(mysql_query($query, $connection))){ while($row = mysql_fetch_assoc($result)) { echo $row['email']; echo $row['username']; echo $row['password']; } }else{ echo "Query Fail"; } Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420397 Share on other sites More sharing options...
payney Posted December 21, 2007 Author Share Posted December 21, 2007 "Query fail" Now I am confused, why is the query not working haha! Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420400 Share on other sites More sharing options...
jitesh Posted December 21, 2007 Share Posted December 21, 2007 Query Will be $query = "SELECT * FROM users WHERE username = '".$username."'"; Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420401 Share on other sites More sharing options...
payney Posted December 21, 2007 Author Share Posted December 21, 2007 Still query fail Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420402 Share on other sites More sharing options...
jitesh Posted December 21, 2007 Share Posted December 21, 2007 (1) Check DB is connected Sucessfully. (2) Check DB is selected Successfully. (3) Echo query and dynamically run in mysql. Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420406 Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 your code should be something like... <?php $username = $_SESSION["authenticatedUser"]; echo $username; if (!empty($username)) { require ('db.php'); if (!($connection = @ mysql_pconnect($hostname, $username, $password))) die("Could not connect to database"); if (!mysql_select_db($databaseName, $connection)) showerror(); $query = "SELECT * FROM users WHERE username = '$username'"; $result = mysql_query ($query, $connection); $intNumRows = mysql_num_rows($result); if ($intNumRows > 0) { while($row = mysql_fetch_assoc($result)) { echo $row['email']; echo $row['username']; echo $row['password']; } } else { echo "No records fetched"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420409 Share on other sites More sharing options...
payney Posted December 21, 2007 Author Share Posted December 21, 2007 I get no records returned. this does not make sense to me. If I hard code the id, username or password it works, but if i assign the variable before the query like: $username = 'payney'; This dosnt work neither. Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420413 Share on other sites More sharing options...
jitesh Posted December 21, 2007 Share Posted December 21, 2007 Check : if (!(mysql_pconnect($hostname, $username, $password))) die("Can not connect"); Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420416 Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 try $query = "SELECT * FROM users WHERE username = '$username'"; echo $query; and tell me what you get Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420417 Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2007 Share Posted December 21, 2007 Uhmmm. $usename is being used by the main code and by the database mysql_connect() function. The value it had from the session is replaced when the 'db.php' file is required/included. Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420418 Share on other sites More sharing options...
payney Posted December 21, 2007 Author Share Posted December 21, 2007 Uhmmm. $usename is being used by the main code and by the database mysql_connect() function. The value it had from the session is replaced when the 'db.php' file is required/included. That is correct, just realised that when returning the query. Whoopsy daisy. Problem solved by changing the name of username to username2. Cheers for all the help and effort guys Quote Link to comment https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/#findComment-420419 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.