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
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.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.