Query () method, query method

Source: Internet
Author: User

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 ();

?>



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.