[RESOLVED] Catchable fatal error: Argument 2 passed to ...() must be an instance of mysqli
Results 1 to 8 of 8

Thread: [RESOLVED] Catchable fatal error: Argument 2 passed to ...() must be an instance of mysqli

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    19

    resolved [RESOLVED] Catchable fatal error: Argument 2 passed to ...() must be an instance of mysqli

    Hi guys,
    I'm still going through these tutorials...going through this one below:
    http://www.youtube.com/watch?v=JUk2e...3367C7&index=8

    I am using mysqli for my database connection (whereas the guy in the video is not, and he is also what seems to me using long complicated queries?...I figure they can be wrote more simpler...), when I am creating the functions to work with mysqli, I am getting an error when I login:


    Catchable fatal error: Argument 2 passed to user_ID_from_username() must be an instance of mysqli, string given, called in C:\wamp\www\LoginRegister\core\functions\users.php on line 49 and defined in C:\wamp\www\LoginRegister\core\functions\users.php on line 33
    Below is my code:

    users.php

    PHP Code:
    function user_ID_from_username$username,mysqli $DB ){ 
        
        
    # sanitize $username (to avoid SQL injection attacks/errors) 
        
    $username $DB->real_escape_string$username ); 

        
    # this is the SQL query we'll use. 
        
    $sql "SELECT `user_id` FROM `users` where `username`='$username'";
        
        
    # execute the query. 
        
    $result $DB->query$sql ); 

        
    # check if there were any rows in the result (if there were no rows, the username does not exist).
         
    return (bool)$result->num_rows;      


    function 
    login$username$password,mysqli $DB ) {
        
    $user_id user_id_from_username($username);
        
        
    #sanitize username and hash password
        
    $username sanitize($username);
        
    $password md5($password);
        
         
    # this is the SQL query we'll use.
        
    $sql "SELECT 1 FROM `users` WHERE `username`='$username' AND `password`='$password'";
        
        
    # execute the query. 
       
    $result $DB->query$sql );
        
        
    # check if there were any rows in the result (if there were no rows, the username and password do not match).
        
    return (bool)$result->num_rows


    This is line 33 -
    PHP Code:
    function user_ID_from_username$username,mysqli $DB ){ 
    This is line 49 -
    PHP Code:
    $user_id user_id_from_username($username); 
    If I comment out line 49 - I can login successfully and it also let's me know if I typed in the worng password....so the "login" function I created is working...its just this "user_ID_from_username" function that is giving me the error...


    Index.php

    PHP Code:
    # database connection 
    $DB = new mysqli'localhost','root','''lr' ); 

    # username to check 
    //$username = 'adrian'; 

    //if( check_username( $username,$DB ) ){ 

        # check_username() returned TRUE. 
    //    print "The username '$username' already exists."; 
    //}else{ 

    //    # check_username() returned FALSE. 
    //    print "The username '$username' is available."; 
    //} 

    if (empty($_POST) === false) { // === checks for type
        
    $username $_POST['username']; //define the $username variable and point it back to the other login.php file where it says - <input type="text" name="username">
        
    $password $_POST['password']; //define the $password variable and point it back to the other login.php file where it says - <input type="password" value="password">

        
    if(empty($username) === true || empty($password) === true) {
            
    $errors[] = 'You need to enter a username and password';  //if the username or password is empty, display the code
        
    } else if(user_exists($username,$DB ) === false) { //WORKFLOW:  we are checking if the user exists, which is calling the user_exists fucntion user_exists in users.php, which then passes in the username, sanitizing it by calling the sanitize function in the general.php, and then we run our query.
            
    $errors[] = 'We can\'t find the username.  Have your registered?';
        } else if(
    user_active($username,$DB ) === false) {
             
    $errors[] = 'You haven\'t activated your account!;';
        } else {
            
    $login login($username$password$DB);
            if (
    $login === false) {
                
    $errors[] = 'That username/password combination is incorrect';
            } else {
                    echo 
    'ok';
                
    // set the user session
                // redirect user to home
        
    }
    }
        

    print_r($errors);
        


    I have an idea what this error is referring to - its saying it wants a proper arguement in the function - I tried putting in the $password variable in the brackets on line 49, but that didn't work....does my sql query in this funciton make sense?

    Thanks in advance,

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,874
    The function user_ID_from_username requires two parameters. You have only provided one in this function call:
    PHP Code:
    $user_id user_id_from_username($username); 
    Also, that function as you have defined it doesn't make any sense. You are fetching a query result from a database and then returning the number of rows returned -- cast as a boolean? A boolean is TRUE or FALSE. Also, the number of rows returned is not the id of a particular user but rather the number of matching records.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    ok, l stepped back to make sure I understand this....and you're right...I copied that query boolean thing from some other code I have so it doesn't make sense...

    here is the code from the tutorial - what is this query saying in english? what does the 0 means at the end of this query? return row 0?

    PHP Code:
    function user_id_from_username ($username) {
        
    $username sanitize($username);
        return 
    mysql_result(mysql_query(SELECT user_id FROM `usersWHERE `username` = '$username'"), 0, 'user_id'); 
    here is what I did to troubleshoot:

    1. since the above query won't work with mysqli connection - I ran a query in phpmyadmin which it returned - 1 (user_id = 1)

    PHP Code:
    SELECT`user_idFROM`usersWHERE`username`='adrian' 
    Is the original query trying to return that result - it should return "1" ? - correct? ...not the number of rows affected right?

    Is that what the mysql_result does? I went to the php man page for mysqli_result - but I am lost by the amount of methods and properties....

    2. to make sure my query and everything worked in php, I created a php file below:

    PHP Code:
    $mysqli = new mysqli'localhost','root','''lr' ); 
    $username 'adrian';

    /* check connection */
    if ($mysqli->connect_errno) {
        
    printf("Connect failed: %s\n"$mysqli->connect_error);
        exit();
    }

    /* Select queries return a resultset */
    if ($result $mysqli->query("SELECT `user_id` FROM `users` where `username`='$username'")) {
        
    printf("Select returned %d rows.\n"$result->num_rows);
        echo 
    $result;

        
    /* free result set */
        
    $result->close();

    - it displayed "Select returned 1 rows" in my browser. I copied this from the php man page for mysql query so it is working....so now I guess my question (besides the ones above) is how to return the actual value (user_id = "1") and not the number of rows affected...I realize that the
    PHP Code:
    $result->num_rows); 
    is doing this...what is $result->num_rows); - a method for mysqli??

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,874
    One very very important thing you need to pay attention to is that mysql and mysqli are not the same thing. The first one (no "i" in it) is the old mysql connection extension. The new one (with the "i" at the end) is the improved one and that's what the i stands for. You should not use the old one because it has been deprecated and will be going away. Do not mix mysql_* and mysql_* functions -- they are totally different and do not work together. Use one or the other. [use the new one!]

    Looking at your tutorial code:
    PHP Code:
    function user_id_from_username ($username) {
        
    $username sanitize($username);
        return 
    mysql_result(mysql_query(SELECT user_id FROM `usersWHERE `username` = '$username'"), 0, 'user_id'); 
    Two things occur to me:
    1) that's not the entire function
    2) It's bad form to feed the result of mysql_query to mysql_result without checking first to see if mysql_query was successful or not.
    3) The last line of that snip is missing a quote right before SELECT and would therefore cause a syntax error.
    That said, your tutorial sounds pretty suspect to me. If you want to understand what parameters you are supplying to mysql_result, check the PHP documentation for mysql_result. The PHP documentation is pretty darn good as software language documentation goes and learning to use it will really benefit you a lot in the long run. And, again, you should be using the mysqli functions and not the old mysql ones. The extra parameters, the zero and "user_id" are being supplied to mysql_result, not mysql_query.

    Always the check of a query to make sure it worked before you try to do anything with it.

    This query:
    PHP Code:
    $result mysql_query("SELECT user_id FROM `users` WHERE `username` = '$username'"
    Says "fetch me all the records from the table "users" where the username column contains the value in my PHP variable $username. mysql_query will return a mysql resource object which you can extract records and values from with other functions like mysql_fetch_assoc. Again, you should be using the mysqli equivalents and not the old ones.

    Functions like mysqli_fetch_assoc take the result of a mysqli_query function call and extract individual records from it. Use the examples in the manual as your starting point. The basic idea is that mysqli_query runs a query that might consist of a million rows and returns a data resource pointer. mysqli_fetch_assoc makes use of that data resource pointer and extracts records one at a time as an associative array.

    Your troubleshooting step is a good one--run the query in phpMyAdmin to see if it's legit. Keep in mind that errors in your PHP may result in syntax mistakes in your sql statements. In your function copied from the php documentation, you are almost there. Instead of echoing the output of $result->num_rows, consider something like this:
    PHP Code:
    // fetch an associative array for each record returned by the result
    while ($row $result->fetch_assoc()) {
      
    // a record was returned! Let's echo it out to see what's in it:
      
    print_r($row);

    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks - that's what I wanted - to have the actual data (the user id) spit out.

    The mysql code I provided is not mine - its the code used in the youtube tutorial - I provided it b/c I am converting it to mysqli.

  6. #6
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,874
    Quote Originally Posted by php_adrian View Post
    The mysql code I provided is not mine - its the code used in the youtube tutorial - I provided it b/c I am converting it to mysqli.
    GIGO
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    how do I resolve a thread?

  8. #8
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,783
    Under thread tools at the top when you are viewing the thread.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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