call to a member function execute() on

91 Views Asked by At

Having trouble with line 27, Don't quite know why as I am very new to PHP/MySQL. Was wondering if anybody can advise me why I am getting the error;

"Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\testscripts\usercreate.php on line 27"

in the following code:

<?php
$name = $_POST["name"];
$psswrd = $_POST["psswrd"];
$username = "root";
$password = "hidden";
$hostname = "localhost";
$table = "testtable";


// create connection to database
// ...


$db= new mysqli($hostname, $username, $password, $table);

// sanitize the inputs
// ...

// create an MD5 hash of the password
$psswrd = md5($psswrd);

// save the values to the database
$sql = "INSERT INTO accounts (name, psswrd) VALUES (:name, :psswrd)";

$stmt = $db->prepare($sql);

$stmt->execute(array(
    ":name" => $name,
    ":psswrd" => $psswrd
));
3

There are 3 best solutions below

0
On

->prepare returns false if an error occurred. Since $stmt->execute is complaining of being called on a non-object, it's reasonable to assume that something went wrong with the query.

Check $db->error.

0
On

Try this :

$db= new mysqli($hostname, $username, $password, $table);
if ($db->connect_errno) {
    throw new Exception($db->connect_error, $db->connect_errno);
}
$psswrd = md5($psswrd);

// save the values to the database
$sql = "INSERT INTO accounts (name, psswrd) VALUES (:name, :psswrd)";

$stmt = $db->prepare($sql);
if (!$stmt) {
    throw new Exception($db->error);
}
$stmt->execute(array(
    ":name" => $name,
    ":psswrd" => $psswrd
));

Show your all exception for better idea of given error.

0
On

First thing, the fourth parameter the MySQLi class takes is the database name, not the table name.

So, change the$table = 'testtable'; to something like this : $dbname = 'dbname';

Also, in your code, you are using named parameters (:name and :passwrd). This won't work because MySQLi doesn't support named parameters. PDO (PHP Data Objects) supports named parameters. If you use the PDO class to connect to the database, your script will work fine!

If you want to connect to the database using the MySQLi class, do this :

$name = $_POST['name'];
$psswrd = $_POST['psswrd'];
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "dbname";


// create connection to database
// ...


$db= new mysqli($hostname, $username, $password, $dbname);

// sanitize the inputs
// ...

// create an MD5 hash of the password
$psswrd = md5($psswrd);

// save the values to the database
$sql = "INSERT INTO `testtable` (id, name) VALUES (?, ?)";

$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $name, $psswrd);

$stmt->execute();

Try that. Use question marks instead of named parameters.

In the bind_param() function, I've written the first parameter as 'ss'. The two 's' here stands for Strings. If you had an integer data, you could have replaced 's' with 'i'.

It's pretty self explanatory as to why there are two 's'. It's because you are binding two variables to the SQL query, both of them are strings. Hence the two 's'.