Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqli throws "Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given" [duplicate]

Tags:

php

mysqli

I know that this code works on another site I've got but it's not playing ball today. I get three warnings:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 33

Warning: mysqli_execute() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 34

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 35

Can someone help me figure this out?

I am having to use htaccess to upgrade to PHP5 if this helps.

$connection = mysqli_connect($hostname, $username, $password, $dbname);

if (!$connection) {
    die('Connect Error: ' . mysqli_connect_error());
}

$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
mysqli_execute($stmt1);
if(mysqli_stmt_affected_rows($stmt1) != 1)
    die("issues");
mysqli_stmt_close($stmt1);
return "new";

EDIT

After some investigation it transpires that the prepare statement doesn't play ball with mysql4. I have created a new mysql5 database but I now get this error when I try to connect:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'localhost:/tmp/mysql5.sock' (1)

Does anyone have any idea as to why this is happening?

like image 233
Drew Avatar asked Feb 09 '10 10:02

Drew


1 Answers

Add more error handling.
When the connection fails, mysqli_connect_error() can tell you more details.
When preparing the statement fails mysqli_error() has more infos about the error.
When executing the statement fails, ask mysqli_stmt_error(). And so on and on...
Any time a function/method of the mysqli module returns false to indicate an error, a) handle that error and b) decide whether it makes sense or not to continue. E.g. it doesn't make sense to continue with database operations when the connection failed. But it may make sense to continue inserting data when just one insertion failed (may make sense, may not).

For testing you can use something like this:

$connection = mysqli_connect(...);
if ( !$connection ) {
  die( 'connect error: '.mysqli_connect_error() );
}

$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
if ( !$stmt1 ) {
  die('mysqli error: '.mysqli_error($connection);
}
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
if ( !mysqli_execute($stmt1) ) {
  die( 'stmt error: '.mysqli_stmt_error($stmt1) );
}
...

For a real production environment this approach is to talkative and dies to easily ;-)

like image 51
VolkerK Avatar answered Sep 26 '22 06:09

VolkerK