Jump to content

mysqli prepared inserts


wright67uk

Recommended Posts

Hi, I'm new to MySQLi and I'm trying to do a basic insert into my database.

 

I've checked the first part of my script and I'm connecting fine.

I've also echoed $mysqli->host_info . "\n"; and everything is as it should be.

 

The below php gives me an error;

Fatal error: Call to undefined method mysqli::bind_param()

 

Why is this? and how can I change my code so that I can do the insert without error?

 

Many thanks.

<?php
define('HOST', '###');
define('USERNAME', '###');
define('PASSWORD', '###');
define('DB', '###');

$mysqli = new mysqli();
$mysqli->connect(HOST, USERNAME, PASSWORD, DB);

$name  = "nametest";//$_POST['name'];
$year  = 2018; 		//$_POST['year'];
$month = "jan"; 	//$_POST['month'];
$wins  = 5;		    //$_POST['wins'];

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$mysqli->prepare("INSERT INTO `compare` VALUES ('?','?','?','?');");
$mysqli->bind_param("sisi", $name, $year, $month, $wins);
$mysqli->execute();
$mysqli->close();
?>
Link to comment
https://forums.phpfreaks.com/topic/276812-mysqli-prepared-inserts/
Share on other sites

when you successfully prepare a query, it returns an instance of the mysqli_stmt class.
 

mysqli_stmt mysqli::prepare ( string $query )

 

 

Return Values

mysqli_prepare() returns a statement object or FALSE if an error occurred.

 

 

 

bind_param is a method of the mysqli_stmt class, not a method of the mysqli class.

 

there are excellent examples in the php.net mysqli documentation.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.