Query () method, query method
Execute SQL commands. No matter how the PHP script interacts with the Mysql database, the process is the same. Create an SQL statement and pass it to the function that executes the query.
The mysqli class provides several methods for executing SQL commands. The most common method is the query () method.
For SQL commands that do not return data, such as insert, update, and delete, the query () method returns true when the SQL command is successfully executed. On this basis, you can use the affected_rows attribute in the mysqli object to retrieve how many records have changed and use the insert_id () method in the mysqli object to return the AUTO_INCREMENT value generated by the last insert command.
If an error occurs during SQL command execution, the query () method returns false. In this case, you can obtain the error number and cause through the errno and error attributes in the mysqli object.
Note: The query () method can only execute one SQL command at a time. To execute multiple commands at a time, you must use the multi_query () method in the mysqli object. If you want to execute an SQL command multiple times with different parameters, the most efficient way is to pre-process the command and then execute it.
<? Php
$ Mysqli = new mysqli ("localhost", "mysql_user", "mysql_pwd", "my_db_name ");
If (mysqli_connect_errno ()){
Printf ("Connection Failed: % s <br>", mysqli_connect_error ());
Exit ();
}
/* Execute the INSERT command and obtain its automatic number value */
If ($ mysqli-> query ("insert into Table Name (column 1, column 2) value ('value 1, value 2 ')")){
Echo "Number of changed records:". $ mysqli-> affected_rows. "<br> ";
Echo "newly inserted ID value:". $ mysqli-> insert_id. "<br> ";
}
$ Mysqli-> close ();
?>