Page 1 of 1

mysqli binding troubles

Posted: Fri Jan 09, 2009 6:41 pm
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?

Re: mysqli binding troubles

Posted: Fri Jan 09, 2009 8:35 pm
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.

Re: mysqli binding troubles

Posted: Sat Jan 10, 2009 2:32 pm
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...