mysqli binding troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
soup2bits
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 6:30 pm

mysqli binding troubles

Post by soup2bits »

Or to be more specific i get this error after trying to run the code bellow:

Fatal error: Call to a member function bind_param() on a non-object

Code: Select all

 
<?php
    $firstName = $_POST["firstName"];
    $lastName = $_POST["lastName"];
    $email = $_POST["email"];
    $zipcode = $_POST["zipcode"];
    $country = $_POST["country"];
    $comment = $_POST["comment"];
        
    $conn = mysqli_connect("localhost", "root", "", "testdb");
    
    $stmnt = $conn->prepare("INSERT INTO testtable (firstName, lastName, email, zipcode, country, comment)
                        VALUES(?, ?)");
 
    $stmnt->bind_param("sssiss", $firstName, $lastName, $email, $zipcode, $country, $comment);
    $stmnt->execute();
    $stmnt->close();
    $conn->close();
?>
 
I've googled for hours now without a solution, I'm about to pull my hair out over this. The general gist of what I've learned is that bind_param() returns False when the prepare statement is incorrect due to incorrect SQL, however I made the changes and still nothing. Anyone have an ounce of clue what I'm doing wrong here?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: mysqli binding troubles

Post by Weirdan »

This should give you a clue:

Code: Select all

 
    // ....
    $stmnt = $conn->prepare("INSERT INTO testtable (firstName, lastName, email, zipcode, country, comment)
                        VALUES(?, ?)");
    if (!$stmnt) var_dump($conn->error);
 
Basically your query is invalid and thus cannot be prepared.
soup2bits
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 6:30 pm

Re: mysqli binding troubles

Post by soup2bits »

Ah, now it finally makes sense.

EDIT: Never mind, the new problem is something completely different, thanks for putting me on the right track though.

EDIT2: Finally, lesson learned, never rely on your book 100%, they have mistakes too. And for any poor soul who'll come across this thread years later:

Code: Select all

 
$stmnt = $conn->prepare("INSERT INTO testtable (firstName, lastName, email, zipcode, country, comment)
                         VALUES(?, ?)");
 
should have been this:

Code: Select all

 
$stmnt = $conn->prepare("INSERT INTO testtable (firstName, lastName, email, zipcode, country, comment)
                         VALUES(?, ?, ?, ?, ?, ?)");
 
I didn't have enough placeholders under VALUES() apparently...
Post Reply