Razor42 Posted April 7, 2014 Posted April 7, 2014 I'm working on a new project using PDO and I'm having some issues... I'm getting the error.. Fatal error: Call to a member function execute() on a non-object in /home/soldiers/public_html/signup.php on line 16 The code in question is.... $query = $db->prepare("INSERT INTO `owners` (`username`, `password`, `email`) VALUES (?,?,?,?)"); $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $query->execute(array($username, $password, $email)); Quote
Dayo Posted April 7, 2014 Posted April 7, 2014 (edited) First thing i notice is you have 4 question marks try this $query = $db->prepare("INSERT INTO `owners` (`username`, `password`, `email`) VALUES (:uname, :upass, :email)"); $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $query->bindParam(":uname", $username); $query->bindParam(":upass", $password); $query->bindParam(":email", $email); $query->execute(); Edited April 7, 2014 by Dayo Quote
Razor42 Posted April 7, 2014 Author Posted April 7, 2014 First thing i notice is you have 4 question marks try this $query = $db->prepare("INSERT INTO `owners` (`username`, `password`, `email`) VALUES (:uname, :upass, :email)"); $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $query->bindParam(":uname", $username); $query->bindParam(":upass", $password); $query->bindParam(":email", $email); $query->execute(); Fatal error: Call to a member function bindParam() on a non-object in /home/soldiers/public_html/signup.php on line 15 Quote
Dayo Posted April 7, 2014 Posted April 7, 2014 Can you post lines 0-15 please (delete all DB connection info)? Quote
Razor42 Posted April 7, 2014 Author Posted April 7, 2014 Lines 1-8. DB connection info is not there as I have a config.php file. <?php require_once('header.php'); if($_GET['action'] == "signup") { $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); $password = $_POST['password']; $username = $_POST['username']; if(empty($email) || empty($password) || empty($username)) { echo '<div class="error">Please fill in all input fields!</div>'; } elseif ($_POST['password'] != $_POST['cpassword']) { echo '<div class="error">The passwords you have entered do not match!</div>'; } else { $query = $db->prepare("INSERT INTO `owners` (`username`, `password`, `email`) VALUES (:uname, :upass, :email)"); $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $query->bindParam(":uname", $username); $query->bindParam(":upass", $password); $query->bindParam(":email", $email); $query->execute(); Quote
Dayo Posted April 7, 2014 Posted April 7, 2014 Where do you set up $db i.e. $db = new PDO("mysql:..."); Quote
Razor42 Posted April 7, 2014 Author Posted April 7, 2014 $db = new mysqli ($settings['host'], $settings['user'], $settings['password'], $settings['database']); Quote
Script47 Posted April 7, 2014 Posted April 7, 2014 (edited) $db = new mysqli ($settings['host'], $settings['user'], $settings['password'], $settings['database']); Your error is there. You are no longer using MySQLi, so you initializing a MySQLi driver makes so sense. $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); I was going to point out the extra ? however Dayo did that. Check this tutorial out. Edited April 7, 2014 by Script47 Quote
john. Posted April 7, 2014 Posted April 7, 2014 Well, there you have the problem. PDO and MySQLI isn't the same thing. You need to create a PDO instance, i.e new PDO($dsn, $username, $password); Your code in the first example is also invalid: $query = $db->prepare("INSERT INTO `owners` (`username`, `password`, `email`) VALUES (?,?,?,?)"); $password = password_hash($_POST['password'], PASSWORD_BCRYPT); $query->execute(array($username, $password, $email)); Remove one of the question marks. Quote
gamble Posted April 7, 2014 Posted April 7, 2014 What's a PDO? The, in my opinion, best way to interact with a database! Quote
sniko Posted April 7, 2014 Posted April 7, 2014 What's a PDO? From the docs themselves The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server. Â PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility. Quote
Samurai Legend Posted April 7, 2014 Posted April 7, 2014 So is it like $db->query? Like from MCCodes V2? Quote
sniko Posted April 7, 2014 Posted April 7, 2014 So is it like $db->query? Like from MCCodes V2? $db is just a class object. IIRC, the McCodes database class is just a wrapper for MySQL, and not an intense abstraction layer like PDO. Quote
Samurai Legend Posted April 7, 2014 Posted April 7, 2014 Alright, I'll be researching about PDO to :P Thanks guys. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.