woodwardmw
woodwardmw

Reputation: 51

bind_param() doesn't seem to work

I have the following code:

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$db = new mysqli($dbhost, $dbuser, $dbpass, 'images_db');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
else{
echo "Connected to database";
}
//filename, mime_type and file_size are columns in the table images
$stmt = $db->prepare("INSERT INTO images (filename, mime_type, file_size) VALUES (?, ?, ?)");
$string1 = 'string 1';
$string2 = 'string 2';
$stmt->bind_param('ssi', $string1, $string2, 123);
$stmt->execute();
$stmt->close();
$mysqli->close();
?>

When I execute the code, nothing gets added to the mysql database. But when I comment out the line

$stmt->bind_param('ssi', $string1, $string2, 123);

and insert the string and integer values directly into the $db->prepare statement (replacing the question marks), it all works nicely and the row is added to the database table.

What am I doing wrong in the bind_param line that is preventing the new row being added to the database?

Upvotes: 3

Views: 5343

Answers (2)

Phil
Phil

Reputation: 165069

mysqli_stmt_bind_param accepts variables (by reference). You cannot use literals. Change your code to

$fileSize = 123;
$stmt->bind_param('ssi', $string1, $string2, $fileSize);

Upvotes: 13

Pradeeshnarayan
Pradeeshnarayan

Reputation: 1235

Please try the variable assignment after bind_param(). It is a passed by reference call. So it will work after also.

$stmt = $db->prepare("INSERT INTO images (filename, mime_type, file_size) VALUES (?, ?, ?)");
$stmt->bind_param('ssi', $string1, $string2, $num);
$string1 = 'string 1';
$string2 = 'string 2';
$num=123;
$stmt->execute();

Upvotes: 0

Related Questions