When PHP writes to the database, call to a member function Bind_param () on a non-object

Source: Internet
Author: User

<?php$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "MyDB";//Create connection $conn = new mysqli ($servername, $username, $password, $dbname);//Detect connection if ($conn->connect_error) {die ("Connection failed:". $conn->connect_error);} Prepare and bind$stmt = $conn->prepare ("INSERT into Myguests VALUES (?,?,?)"); $stmt->bind_param ("SSS", $firstname, $lastname, $email);//Set parameters and Execute $firstname = "John"; $lastname = "Doe"; $email = "[ Email protected] "; $stmt->execute (); $firstname =" Mary "; $lastname =" Moe "; $email =" [email protected] "; $stmt Execute (); $firstname = "Julie"; $lastname = "Dooley"; $email = "[email protected]"; $stmt->execute (); echo "New Records Created successfully "; $stmt->close (); $conn->close ();? >

 Run Times Error:

Bind_param () on a non-object

Reason:
The second argument to Bind_param is the one that is passed by reference.
Written directly to the string, which is not allowed in PHP 5.3 and later

Workaround:
$stmt = $conn->prepare ("INSERT into Myguests VALUES (?,?,?)");
Change to $stmt = $conn->prepare ("INSERT into Myguests (firstname, LastName, email) VALUES (?,?,?)");

When PHP writes to the database, call to a member function Bind_param () on a non-object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.