php function mysql_query batch execution of multiple SQL statements

Source: Internet
Author: User

Under normal circumstances, PHP's mysql_query function is not able to execute multi-sentence CREATE table and other statements in bulk;

And some scenarios, such as having a SQL backup file, or creating a data table structure when the program is initialized, you need to batch execute the SQL statements in the file;

To solve this problem, you can use the Explode function to press '; ' The SQL statement is split into groups, and then the mysql_query can be executed by looping, because every SQL is '; ' The end of the number;

But consider that there may be entity symbols in the SQL statement   Or, if you press '; ' It is easy to break the fire, so it is more reliable to use preg_split.

Sample environment:

SQL file:./thinkbjy.sql

$sql _array=preg_split ("/;[        \r\n]+/", file_get_contents ('./thinkbjy.sql '));           foreach ($sql _array as $k = + $v) {mysql_query ($v, $link); Echo mysql_error (). '        <br> '; }

Perform echo mysql_error (). ' <br> '; To see if there are errors;

650) this.width=650, "alt=" Bai Jun Remote Blog "src=" http://www.baijunyao.com/Upload/image/ueditor/20150712/1436709823663876. JPG "title=" Bai Jun Haruka Blog "/>


php function mysql_query batch execution of multiple SQL statements

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.