justlukeyou Posted November 11, 2012 Share Posted November 11, 2012 Hi, The code at the bottom of the post works fine as it updates memberrs the ID by using the session UserID. However I am now trying to echo using a query against the ID. When I try to use the the query just below whatever format I try it creates an error. Can anyone advise what I am doing wrong. <?php $query = mysql_query("SELECT * FROM users WHERE id= .$_SESSION['userID']"; while($row = mysql_fetch_array($query)) { ?> <?php echo $row['surname']; ?> <?php } ?> $query = mysql_query("SELECT * FROM users WHERE id=" .$_SESSION['userID'] { $sql = "UPDATE users SET {$values} WHERE id=".$_SESSION['userID']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/ Share on other sites More sharing options...
Andy123 Posted November 11, 2012 Share Posted November 11, 2012 (edited) Either you are not pasting your actual code or you are getting parse errors. You were missing a closing parenthesis for your method call and you had a period in there which made your SQL query invalid. $query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['userID']); Edited November 11, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/#findComment-1391682 Share on other sites More sharing options...
jazzman1 Posted November 11, 2012 Share Posted November 11, 2012 (edited) You are missing the last brace of the mysql_query function. EDIT: Andy123, sorry for my post Edited November 11, 2012 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/#findComment-1391683 Share on other sites More sharing options...
justlukeyou Posted November 11, 2012 Author Share Posted November 11, 2012 Brilliant thanks guys, now when I am logged in it echoes the company. However when I am not logged in it creates a very messy error message. <div class="updatecurrentcell"> <?php $query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['userID']); while($row = mysql_fetch_array($query)) { echo $row['aboutcompany']; } ?> </div> I am trying to add an else so when I am not logged in who echoes "blank". Can anyone advise how I can use this code when I am not logged in to leave an empty space? <div class="updatecurrentcell"> <?php $query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['userID']); while($row = mysql_fetch_array($query)) { echo $row['aboutcompany']; } else { echo 'blank'; } ?> </div> Message: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/site/update.php on line 175 Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/#findComment-1391687 Share on other sites More sharing options...
Andy123 Posted November 11, 2012 Share Posted November 11, 2012 mysql_query() returns FALSE if an error occurred - for instance if you have an error in your SQL query. Your variable $query contains the boolean value FALSE, and you are passing it to mysql_fetch_array(), which is looking for a resource parameter, not a boolean value. Therefore you should test to see if the query was executed correctly first. $query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['userID']); // Check for errors if ($query) { // No errors if (mysql_num_rows($query) > 0) { // No error and at least one row was returned // Loop through the rows; at this point, $query is a resource while ($row = mysql_fetch_array($query)) { // Do things } } else { // No error executing the query, but no rows returned } } Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/#findComment-1391700 Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2012 Share Posted November 11, 2012 And use a conditional control structure to disallow the query execution in the event the necessary values aren't present. Quote Link to comment https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/#findComment-1391701 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.