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

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

  • 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!

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

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);
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.