简体   繁体   中英

PHP PDO error messages

So I'm not too good in programming but I'm trying to make a login for my site. I've researched a bit and PDO was supposed to be safest for calling mySQL queries.

The problem is I don't get it. I've created a function and got this 2 errors. First is probably related to another function in functions.php but I'm not sure why. I've read something about the second one but did not get what I'm supposed to do.

Errors:

1: Warning: Missing argument 1 for userData(), called in page.php on line 34 and defined in functions.php on line 164

2:Fatal error: Call to a member function prepare() on a non-object in functions.php on line 165

This is the function I'm trying to run

    function userData(){
        if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ? LIMIT 1")) {
            $stmt->bind_param('s', $_SESSION['username']); 
            $stmt->execute();  
            $stmt->store_result();
            $stmt->bind_result($username);
            $fetch_stmt=$stmt->fetch();

            if ($stmt->num_rows == 1) {
                $username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);
                $_SESSION['username'] = $username;
                $email=$fetch_stmt['email'];
                return array($username,$email);
            } else {
                return false;
            }
        }

    }

This function should return an array of user data from database.

First, as I have already mentioned in comments, you're using mysqli ant not PDO .

But it doesn't have anything to do with you errors.

Not sure about the missing argument error, but it seems like you have an argument defined in userData , but calling it without the argument.

The second error is about the variable scope . Your connection handler, here it is $mysqli is undefined inside the function userData , because it tries to find a local variable $mysqli and fails. You have to either pass the handler inside the function as a parameter, or restructure your code in a way, that you can get the handler from the other place.

Try this out and see if it helps:

    <?php 

    //DATABASE CONNECTION CONFIGURATION:
    defined("HOST")     or define("HOST",   "localhost");           //REPLACE WITH YOUR DB-HOST
    defined("DBASE")    or define("DBASE",  "_TEST_");              //REPLACE WITH YOUR DB NAME
    defined("USER")     or define("USER",   "root");                //REPLACE WITH YOUR DB-USER
    defined("PASS")     or define("PASS",   "root");                //REPLACE WITH YOUR DB-PASS


    function getUserData(){
        $dbh            = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare("SELECT * FROM users WHERE username = :uName LIMIT 1");
        $stmt->bindParam(':uName', $_SESSION['username']);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_OBJ);

        //YOU MAY JUST RETURN $result & ACCESS ALL DATA IN IT USING OBJECTION ORIENTATED APPROACH LIKE: $email = $result->email;
        if(!empty($result)){
            //return array($_SESSION['username'], $result->$result->email);
               return $result;
        }                
        return null;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM