Php + mysqli method for batch query of multiple tables _ php skills

Source: Internet
Author: User
This article describes how to query data in multiple tables in batches using php + mysqli, and describes how to use functions such as multi_query, store_result, and more_results, for more information about how to query multiple tables in batches using php + mysqli, see the following example. Share it with you for your reference. The specific implementation method is as follows:

Note that two new functions multi_query and store_result are used. the code is as follows:

The code is as follows:

<? Php
// 1. create a database connection object
$ Mysqli = new MySQLi ("localhost", "root", "123456", "liuyan ");
If ($ mysqli-> connect_error ){
Die ($ mysqli-> connect_error );
}
$ Mysqli-> query ("set names 'gbk '");
// 2. query multiple database tables
$ Sqls = "select * from news limit 10, 4 ;";
$ Sqls. = "select * from user ;";
// 3. execute and process the result
If ($ res = $ mysqli-> multi_query ($ sqls )){
// Note: Unlike $ mysqli-> query (), a Boolean value is returned.
Do {
$ Result = $ mysqli-> store_result (); // The resource object of the result set is actually returned here. if the result fails, false is returned;
While ($ row = $ result-> fetch_assoc ()){
Foreach ($ row as $ key => $ value ){
Echo "-- $ value --";
}
Echo "";
}
$ Result-> free ();
If ($ mysqli-> more_results () {// You can check whether a result set exists.
Echo "---------- query the data of the next table ---------------
";
}
} While ($ mysqli-> next_result (); // next_result () returns true or false;
}
// 4. close the database connection
$ Mysqli-> close ();
?>

I hope this article will help you with php programming.

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.