Jump to content

Recommended Posts

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'];



?>

Link to comment
https://forums.phpfreaks.com/topic/270562-query-using-id-against-session-id/
Share on other sites

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 by Andy123

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

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
}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.