2

I have just come back to a project that was working fine a month ago. Today I have found im getting a "Warning: mysqli::query(): Couldn't fetch mysqli" error and I have no idea why. I have tried changing the if statement but I still get the same result.

php page

<?php
include_once 'db_connect.php';
include_once 'functions.php';

sec_session_start();

error_reporting(E_ALL); ini_set('display_errors', 1);

$email = $_SESSION['email'];
$name = $_SESSION['name'];
$hash = $_SESSION['hash'];

$password = $hash;

echo $name;
echo '<br>';
echo $email;
echo '<br>';
echo $hash;
echo '<br>';

$sql = "INSERT INTO signed_up(`name`, `email`, `password`) VALUES ('$name', '$email', '$password')";

if ($mysqli->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $mysqli->error;
}
//if ($result = $mysqli->query("INSERT INTO `signed_up`(`name`, `email`, `password`) VALUES ('$name', '$email', '$password')")){
//
//header('location: ../register_success.php');
////  $result->close();
//
//}
//else {
//  echo "error";
//}
?>

The error im getting

you have succesfuly connected to the database

gptgeoff

up666315@myport.ac.uk

$2y$10$JUi/eSNwbTnM4ZyWetEL6.ELSepLPKLkEiJdi4WYMyp/sEEAk9hKe

Warning: mysqli::query(): Couldn't fetch mysqli in /home/sites/unitest.co.uk/public_html/includes/register_user.inc.php on line 24 Warning: main(): Couldn't fetch mysqli in /home/sites/unitest.co.uk/public_html/includes/register_user.inc.php on line 27 Error: INSERT INTO signed_up(name, email, password) VALUES ('gptgeoff', 'up666315@myport.ac.uk', '$2y$10$JUi/eSNwbTnM4ZyWetEL6.ELSepLPKLkEiJdi4WYMyp/sEEAk9hKe')

The db_connect.php file works because I'm getting the "you have succesfuly connected to the database" displaying, the session variables are being passed and echoed out in both the echo statements and in the values of the $sql query so I am unable to understand why this has stopped working, can anyone advice please

db_connect.php

<?php
include_once 'psl-config.php';   // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

if(!$mysqli)  {
die('error connecting to database');    
}
echo 'you have succesfuly connected to the database'.'<br/><br/>';

?>
Tiny
  • 363
  • 2
  • 6
  • 20
  • do you have some part of the code which actually closes the connection? – Kevin Apr 16 '15 at 11:19
  • Please use Statements like here: http://php.net/manual/de/mysqli.quickstart.prepared-statements.php – PKeidel Apr 16 '15 at 11:19
  • check `$mysqli` variable in `db_connect.php` and `functions.php` see if its getting closed somewhere – Muhammad Bilal Apr 16 '15 at 11:21
  • @Ghost yes in my original if satement I did have the close statement, When I searched this error it was suggested that a close statement could have been my issue so commented it out, that failed to resolve the issue so I then tried a different if staement which also failed to resolve the issue. – Tiny Apr 16 '15 at 11:23
  • try moving `include_once 'db_connect.php';` just before if statement and see if it resolves the issue. – Muhammad Bilal Apr 16 '15 at 11:25
  • @madsforstrength the db_connect doesnt have a close staement and the functions.php has no bearing on database connection – Tiny Apr 16 '15 at 11:27
  • @madsforstrength tried that, no difference – Tiny Apr 16 '15 at 11:29
  • @user3370876 okay, have you checked the instance of your connection all through out the process of insertion. might want to check it out also. use `$mysql->error` accordingly – Kevin Apr 16 '15 at 11:31
  • Are you using `mysqli_query` somewhere? – Muhammad Bilal Apr 16 '15 at 11:33
  • @Ghost this was working fine when I last tested it, I have tried to check all the parameters needed for this to work, as you can see in the error described the connection to the database is ok, as im getting the database connection success, all the session variables are echoing correctly, it is the sql insertion that is failing on the mysqli fetch – Tiny Apr 16 '15 at 11:35
  • by the way, is that really part of your query, notice this one `9hKe')*` its stated on the error message. there's a stray asterisk. whats up with that :D anyway, i'd suggest use prepared statements instead as you are already using MySQLi API which supports it – Kevin Apr 16 '15 at 11:37
  • @Ghost When putting the error into the question I added it as italics and that is the tag stack gives it – Tiny Apr 16 '15 at 11:43
  • @user3370876 oh okay, now thats me being nitpicky lol. anyway, prepared statements, thats the last suggestion i'l make, everything else is exhausted – Kevin Apr 16 '15 at 11:45
  • I have also done a sql query on the database in PHP Myadmin and it inserted fine – Tiny Apr 16 '15 at 11:47
  • @user3370876 check and remove `mysqli_query()`, because mixing both oop and procedural calls can also result in this error – Muhammad Bilal Apr 16 '15 at 11:49
  • 1
    I would like to thank everyone for their help, I have now resolved the issue which was in the end my stupid error, at some point working on a different project I think I saved a different db_connect file over this projects one so therefore the login details were all wrong, – Tiny Apr 16 '15 at 12:23
  • Does this answer your question? [Should we ever check for mysqli\_connect() errors manually?](https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually) – Dharman Jan 15 '20 at 21:37

1 Answers1

0

This line:

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

creates an object. It is an instance of the class mysqli. An object will always be true. Your if statement checks if $mysqli is false. This if statement is wrong, because whether the connection was successful or not the object will never be false.

Do not check for connection errors manually. Your connection failed and when it did PHP threw a warning. Please check your PHP configuration for error reporting, because you have probably warnings silenced. Also please enable mysqli error reporting. Insert this line before creating an instance of mysqli.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Dharman
  • 30,962
  • 25
  • 85
  • 135