Couldn't fetch mysqli

database

#1

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

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

date_default_timezone_set('America/Chicago');
              $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);


              $stmt->execute();
              $stmt->close();
              $con->close();

Could anyone help me out?


#2

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

like

to

lik

Or even

like

to

lkie

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.


#3

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


#4

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


#5

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

<?php
declare(strict_types=1);
ini_set('display_errors', 'TRUE');
error_reporting(-1); // MAXIMUM ERROR REPORTING

// TO BE REMOVED AFTER ESTABLISHING THE VERSION
echo phpversion(); die;

date_default_timezone_set('America/Chicago');

$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:


#6

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.


#7

They're the exact same. No changes


#8

\

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


#9

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.


#10

Errors:

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

#11

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.


#12

You can view my entire code here.

https://pastebin.com/zb3d6QJc

Yes, db.php works correctly. I checked.


#13

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.


#14

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.


#15

Thanks for your help. I have fixed the issue