Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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:

    Code:
    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?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,479
    Thanks
    0
    Thanked 635 Times in 625 Posts
    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?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    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:
    Code:
    $query = "SELECT * FROM pages WHERE subject_id ='$pages_id'";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •