Couldn't fetch mysqli

I have spent a freaking month trying to figure this out, looking on multiple forums, checking documentation, etc. Probably should’ve asked sitepoint a while ago. Basically I get two errors along the lines of:

Couldn't fetch mysqli

and this one:

PHP Fatal error:  Uncaught Error: Call to a member function bind_param()

Here’s my HTML form:

<form method="post">
                   <input type="text" placeholder="Post Title" class="input-box top-margin" name="title" required maxlength="40">
                   <input type="text" placeholder="Author Name" class="input-box top-margin" name="author" required>
                   <input type="text" placeholder="Short Preview Text" class="input-box top-margin" name="ptext" required>
                   <input type="text" class="top-margin input-box top-margin" placeholder="Insert an image path..." name="imagepath" required>
                   <input type="text" class="top-margin input-box top-margin" placeholder="Enter Tags..." name="tags" required>
                   <input type="text" class="top margin input-box top-margin" placeholder="Enter Hidden Tags..." name="hidden_tags" required>
                   <textarea class="input-box top-margin" style="height:150px;" name="body" required></textarea>
                   <input type="submit" value="Launch Post" class="long-btn btn-def top-margin bottom-margin" name="submit">

Here’s my PHP code which seems to be creating the fuss:

              $title = $_POST['title'];
              $author = $_POST['author'];
              $ptext = $_POST['ptext'];
              $imgpath = $_POST['imagepath'];
              $tags = $_POST['tags'];
              $hidden_tags = $_POST['hidden_tags'];
              $body = $_POST['body'];
              $post_date = date('Y/m/d H:i:s');

	      $stmt = $con->prepare("INSERT INTO posts (title, author, prev_text, img_path, tags, hidden_tags, body, post_date, pad) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
	      $stmt->bind_params("sssssssss", $title, $author, $ptext, $imgpath, $tags, $hidden_tags, $body, $post_date, $pad);


Could anyone help me out?

Where’s your $con variable? Also, don’t close your database connections. Let PHP do this for you. Another thing, check to make sure that you didn’t misspell anything. Double check both the syntax and your database column. Go step by step with every single letter and compare them to make sure they aren’t different. A simple




Or even




Will cause your query to fail.

Also, where is $pad? I see you are referencing the variable, but I don’t see you define it anywhere.

Ahh. I required the $con variable at the top of the page. Sorry about that.

Anyway, I fixed the pad variable, but I still get loads of errors

That doesn’t help with anything. Please post the exact errors you are getting here.

1 Like

Please report back on the PHP Version being used then remove echo phpversion(); die;

Try adding these lines at the top of the page if you are using PHP7 otherwise remove the declare(strict_types=1);

ini_set('display_errors', 'TRUE');
error_reporting(-1); // MAXIMUM ERROR REPORTING

echo phpversion(); die;


$title        = $_POST['title']       ?? '$title NOT SET';
$author       = $_POST['author']      ?? '$author NOT SET';
$ptext        = $_POST['ptext']       ?? '$ptext NOT SET';
$imgpath      = $_POST['imagepath']   ?? '$imgpath NOT SET';
$tags         = $_POST['tags']        ?? '$tags NOT SET';
$hidden_tags  = $_POST['hidden_tags'] ?? '$$hidden_tags NOT SET';
$body         = $_POST['body']        ?? '$body NOT SET';
$post_date    = date('Y/m/d H:i:s')   ?? '$post_date NOT SET';

// previous script to go here

Choosing to write scripts without taking advantage of the excellent PHP error_reporting facilities is like driving blindfolded - it is only a matter of time before you crash :frowning:

This doesn’t look correct to me

 $stmt->bind_params("sssssssss", $title, $author, $ptext ...

although the error message doesn’t match.

And here

$post_date = date('Y/m/d H:i:s');

if all you want to do is record the date/time when a row is inserted, have a default value of NOW() set for your column and leave it out of the query altogether.

1 Like

They’re the exact same. No changes

[quote=“droopsnoot, post:6, topic:301296”]
This doesn’t look correct to me

$stmt->bind_params(“sssssssss”, $title, $author, $ptext …

You’re right. I seemed to have had bind_params instead of the proper bind_param

That still does not help solve your problem. Could you please post the exact errors from your error log so we can determine what is really going on.


PHP Warning:  mysqli::prepare(): Couldn't fetch mysqli in /home/willhoffman/public_html/admin/index.php on line 142

PHP Fatal error:  Uncaught Error: Call to a member function bind_params() on null in /home/willhoffman/public_html/admin/index.php:143

Stack trace:
#0 {main}
  thrown in /home/willhoffman/public_html/admin/index.php on line 143

Change params to param. Then add in $con because I have yet to see it in your code. You have said that you added it and made changes, yet I don’t see that any where in your errors. You still get the same error message for the thing you were told to change.

You can view my entire code here.

Yes, db.php works correctly. I checked.

Uncaught Error: Call to a member function bind_params() on null

This basically means that $con is not defined. And that happens because you have mysqli_close($con); on line 107. You can’t query any more after you’ve closed the connection. Remove that line and it should work again.

Although I must note in general your code could really do with some cleaning. Like don’t insert stuff halfway through the page, but at the top of the script. And then redirect to the page itself to prevent repeated POST requests.


Which is pretty much what I have said in post #2. The error saying that the mysqli connection is nulled pretty much gave it away. That was why I was intent on finding out about the $con variable.

1 Like

Thanks for your help. I have fixed the issue

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.