Jump to content

Need help with Prepared statement

Go to solution Solved by mikosiko,

Recommended Posts

Im new to prepared statement and after a lot of research and reading, i still cant get it right. I have created a small database with user_id auto, name, lastname, and tried to make  an insert with prepared statement. I have tried it several ways but still cant spot the error. Here is my 1 code.

$mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem');
if (!($stmt = $mysqli->prepare("INSERT INTO pps(name) VALUES (?)"))) {
     echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;

/* Prepared statement, stage 2: bind and execute */
$username = "john";
if (!$stmt->bind_param($username)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;

This above code, gives me Warning: Wrong parameter count for mysqli_stmt::bind_param() in line 9. 

Binding parameters failed: (0) Execute failed: (2031) No data supplied for parameters in prepared statement. 

Note: Line 9  is this : $username = "john";  


Then i tried this code which also fails. 


$mysqli = new mysqli ('localhost', 'root', '','lr') or die ('there is a problem');
$query = "INSERT INTO pps (name, lastname) VALUES (?,?,)";
$stmt = $mysqli->prepare($query);

$val1 = 'John';
$val2 = 'Lastname';
$stmt->bind_param("ss", $val1, $val2);

/* Execute the statement */

$val1 = 'Mark';
$val2 = 'Lastnamel';

/* Execute the statement */

/* close statement */

This code results in Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\PreparedStatement\test.php on line 9. 

How can i correct this ? 

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.

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.