Jump to content
MakeWebGames

PDO Problem


Razor42

Recommended Posts

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));
Link to comment
Share on other sites

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 by Dayo
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

$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 by Script47
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...