Query () method

Source: Internet
Author: User
Execute SQL command, regardless of the PHP script to interact with the MySQL database, the process is the same, create an SQL statement, and then passed to the function to execute the query.
Several methods of executing SQL commands are available in the Mysqli class, the most common of which is the query () method.
For SQL commands that do not return data for INSERT, UPDATE, Delete, and so on, the query () method returns True when the SQL command executes successfully. Based on this, the Affected_rows property in the Mysqli object can also be used to hunt for how many records have changed and use the insert_id () method in the Mysqli object to return the Auto_increment number value generated by the last insert command.
If an error occurs in the execution of the SQL command, the query () method returns False, and the error number and cause of the error can be obtained by errno, the error property in the Mysqli object.
Note: The query () method can execute only one SQL command per call, and if you want to execute multiple commands at once, you must use the Multi_query () method in the Mysqli object. If you want to execute a SQL command multiple times with different parameters, the most efficient way to do this is to first pre-process the command and then execute it.

$mysqli =new mysqli ("localhost", "Mysql_user", "Mysql_pwd", "my_db_name");

if (Mysqli_connect_errno ()) {

printf ("Connection failed:%s
", Mysqli_connect_error ());
Exit ();

}

/* Execute the INSERT command and get its AutoNumber value */

if ($mysqli->query ("INSERT into table name (column 1, column 2) value (' Value 1, value 2 ')") {
echo "changed number of records:" $mysqli->affected_rows. "
";
echo "Newly inserted ID value:" $mysqli->insert_id. "
";
}


$mysqli->close ();
?>

The above describes the query () method, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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