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.