Jump to content

Call to a member function stmt_init() on a non-object


jeppers

Recommended Posts

i am really struggling and i have no idea  what to do.

 

Call to a member function stmt_init() on a non-object

 

on this code.

if (isset($_POST['insert'])) {
  require_once('includes/connection.php');
  // initialize flag
  $OK = false;
  // create database connection
  $conn = dbConnect('write');
  // initialize prepared statement
  $stmt = $conn->stmt_init();
  // create SQL

 

i am using a book but i have tried it many ways and the same error occurs.

 

i have looked on forums and they say that i have not set up  the users correctley. but for a test i give my user all the permitions that i could and same problem.

 

the error occurs on this line

 

$stmt = $conn->stmt_init();

 

with the error message stating 

 

Call to a member function stmt_init() on a non-object

 

please if you have seen this before could you help

 

thanks in advance

 

<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
  $host = 'localhost';
  $db = 'gallery';
  if ($usertype  == 'read') {
$user = '****';
$pwd = '*****';
  } elseif ($usertype == 'write') {
$user = '*****';
$pwd = '******';
  } else {
exit('Unrecognized connection type');
  }
  if ($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
  }
}
?>

 

there it is i just don't no what i am doing wrong. i have tried so may things

 

please help

  • 3 weeks later...

I just found this thread from a search. I'm using the same book as the OP and I'm having the same exact problem. Using the same exact code. Does anybody know what the fix for this might be? If $conn returns boolean true is that why we get the error "Fatal error: Call to a member function stmt_init() on a non-object"? What type of result should $conn return so that this error won't get thrown when we use the stmt_init() function?

 

Thanks for any follow up!

The problem is that putting or die() on the end of a new mysqli() statement won't ever die because a new mysqli() statement will always return an object even if the connection fails. You would test if an OOP mysqli connection worked or failed by using mysqli_connect_error() or by using mysqli->connect_error in those versions of php where they fixed the mysqli code to work.

 

Having error_reporting set to E_ALL (or to a -1) would be producing a warning message at the failing new mysqli() statement.

 

Also, by putting the or die() on the end of the new mysqli() statement, the value returned is always converted to a bool(true), even with a successful connection, and the or die() should be removed, even if you don't change the code to use mysqli_connect_error().

Very cool. Thanks for your help, PFMaBiSmAd. And thanks for clearly explaining this. Hopefully the OP will come back and see this. Your solution works. I can successfully register a user and add them to a database. So, here's what I did. I changed the last IF statement in the dbConnect function to this:

 

  if ($connectionType == 'mysqli') {
return new mysqli($host, $user, $pwd, $db);
  } elseif ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.