Home  >  Article  >  Backend Development  >  query() method

query() method

WBOY
WBOYOriginal
2016-08-08 09:26:142585browse

Executing SQL commands, no matter how you interact with the Mysql database through PHP scripts, the process is the same. Create a SQL statement and then pass it to the function that executes the query.
      The mysqli class provides several methods for executing SQL commands, the most commonly used of which is the query() method.
               For SQL commands such as insert, update, delete, etc. that do not return data, the query() method returns true when the SQL command is successfully executed. On this basis, you can also retrieve how many records have changed through the affected_rows attribute in the mysqli object, 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 while executing the SQL command, the query() method will return false. At this time, the error number and error reason can be obtained through the errno and error attributes in the mysqli object.
Note: The query() method can only execute one SQL command per call. 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 is to do some preprocessing on that command and then execute it.

$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 get its automatic number value*/

if($mysqli->query("insert into table name (Column 1, Column 2) value ('value 1, value 2')")){
   echo "Number of records changed: ".$mysqli->affected_rows."
";
   echo "New insert ID value: ".$mysqli->insert_id."
";
}


$mysqli->close();
?>


The above introduces the query() method, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn