View Full Version : Recurring Error: "mysql_Query() Expects Parameter 2 To Be Resource"

11-20-2011, 05:49 AM
I created a function, which keeps returning the error "mysql_query() expects parameter 2 to be resource" when I open index.php.

Here is my function:

function get_pages_by_id($pages_id) {

global $connection;

$query = "SELECT * FROM pages WHERE subject_id =" . $pages_id;

$result = mysql_query($query, $connection) ;

return $result;


If I get rid of $connection (which I've been told is not necessary), the error above disappears, however, I am returned with the error "No database selected" instead.

or die mysql_error() has not been useful in this case..

Both connecting to my server and selecting a database were successful (tested and confirmed using sql_error). They appear as followed.
I am not sure as to whether this is relevant, but the function is contained in functions.php which is included in index.php.

Please help?

11-20-2011, 06:11 PM
The connection to the database isn't getting passed from where it is established into the $connection field in the global namespace.

In the function where you make the connection do you have $connections defined as global?

11-20-2011, 06:19 PM
While $connection is not needed in this line: $result = mysql_query($query, $connection) ;

It may not be the problem. running this on my DB also gives me errors, but corrected with

a) removing the $connection as stated above and
b) using this line:

$query = "SELECT * FROM pages WHERE subject_id ='$pages_id'";