php statement question. (mysqli)

2019-09-03 10:32发布

问题:

$db = new mysqli('localhost','root','nere','deneme');

if ($db->error)
exit();

$stmt = $db->prepare("INSERT INTO deneme VALUES (?,?,?)");

$stmt->bind_param('ssi',$adi,$soyadi,$no);

$adi='recep';
$soyadi='saban';
$no=5;

$stmt->execute();

i got error.

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\rock\index.php on line 10

what I am doing wrong?

回答1:

it should be something like below. Also you should assign the values to the variables before passing it to bind_param()

if ($db->error)
exit();

if ($stmt = $db->prepare("INSERT INTO deneme VALUES (?,?,?)")) {
$adi='recep';
$soyadi='saban';
$no=5;

$stmt->bind_param('ssi',$adi,$soyadi,$no);


$stmt->execute();
}

It's also a good idea to tell which columns should be populated in your database:

INSERT INTO deneme (column1, column2, column3) VALUES (?,?,?)


回答2:

Looks like your prepare statement is failing, and $stmt is false (you should add some checking there). Does table deneme actually exists?



标签: php mysqli