views:

67

answers:

4

Here is my error:

Warning: mysql_query() expects parameter 2 to be resource, null given...

This refers to line 23 of my code which is:

$result = mysql_query($sql, $connection)

My entire query code looks like this:

$query = "SELECT * from users WHERE userid='".intval( $_SESSION['SESS_USERID'] )."'"; 
                $result = mysql_query($query, $connection)
                or die ("Couldn't perform query $query <br />".mysql_error());

                $row = mysql_fetch_array($result);

I don't have a clue what has happpened here. All I wanted to do was to have the value of the users 'fullname' displayed in the header section of my web page. So I am outputting this code immediately after to try and achieve this:

echo 'Hello '; echo $row['fullname'];

Before this change, I had it working perfectly, where the session variable of fullname was echoed $_SESSION['SESS_NAME']. However, because my user can update their information (including their name), I wanted the name displayed in the header to be updated accordingly, and not displaying the session value.

A: 

You have two ways of doing this, you need to use mysql_connect to connect to your database, you can pass this to mysql_query if you desire, if you don't pass anything to mysql_query PHP uses the last link opened from mysql_connect

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

$sql = "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

Have you connected to your database? If so please show this code too.

For now, just try removing the $connection variable, like this:

$result = mysql_query($query);

And see where that gets you.

ILMV
+1  A: 

Your $connection variable is NULL that's what your error message is referring to.

Reason being is that you have not called mysql_connect. Once called it will assign you a resource where you can set it to the $connection variable, thus being non-null.

As an example:

$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');
// now $connection has a resource that you can pass to mysql_query
$query = "SELECT * from users WHERE userid='".
                                         intval( $_SESSION['SESS_USERID'] )."'"; 
$result = mysql_query($query, $connection)
Anthony Forloney
Thanks, I didn't realise I needed the connection because before I was simply using the session to retrieve the data... but now with the information coming from the database, i simply did a <?php require_once('config.php');Thanks again.
Yvonne
Not a problem, anytime, glad I could help.
Anthony Forloney
A: 

$connection is assigned the value of the database connection resource id. You don't have that in your script, so the value of $connection is NULL, and that is why you are getting the error. You need to connect to the database before using mysql_query(). You should be okay after that.

Salil
A: 

You need to do:

$connection=mysql_connect('host','user','pass');
if($connection === false) {
 echo "Error in connection mysql_error()";
}
codaddict