user6044058 user6044058 - 1 year ago 80
PHP Question

Mysqli bind_param error

I have a issue with my prepared statement, I have searched through stack for the answer and tried many off them them to no avail, I have check the insert statement which is correct, I have also tried using the statement as a regular statement which seems to work,
The error I get is as below

Fatal error:call to function bind_param()on a non object in ** on line 23

When I use a normal query on the first query the second does not show the error

The code is

public function reg_user($name,$username,$password,$email){
$password = password_hash($password);
$stmt = $this->db->prepare("SELECT uid FROM users WHERE uname= ? OR uemail = ?");
$stmt->bind_param("ss", $username,$email);
$count_row = $stmt->num_rows;
if($count_row == 0){
$stmt = $this->db->prepare("INSERT INTO users (uname,upass,fullname,uemail) VALUES(?,?,?,?)");
$stmt->bind_param("ssss", $username,$password,$name,$email); //line 23
$return = $stmt->execute();
return $return;
} else{
return false;

Answer Source

Add these two lines of code after the first $stmt->execute

$result = $stmt->get_result();
$count_row = $result->num_rows;

The reason why your code fails is because you are trying to get the number of rows from a Prepared Statement object instead of a Result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download