search
Homephp教程php手册PHP执行批量mysql语句的解决方法

PHP执行批量mysql语句的解决方法

Jun 13, 2016 am 11:53 AM
mysqlphpimplementbatchmethodhaveofsolvestatementneed

当有多条mysql语句连起来需要执行,比如

$sqls= “insert table a values(1,2); insert table a values(2,3);”

需要执行的话php中可以使用的方法有三个:

mysql_query

pdo

mysqli

三种方法当sqls语句没有问题的时候都是可以的。

但是

当sql语句是错误的时候会出现问题
第一条sql错误:三个方法都返回false

第一条sql正确,第二条sql错误:mysql_query、pdo、 mysqli:query也是返回true。所以这个时候你是没法判断你的sqls是否有那条语句是错误的。


解决这种办法有几个招:

1 解析sql语句
将每条sql都拆分开来执行。这样每个语句分开执行就解决了。但是这种方法多出了好几种方法,所以不可取。

2 将sqls语句保存为文本
使用cmd执行命令 mysql…. .

3 使用mysqli::multi_query方法
这个方法可以执行多条sql语句,然后使用mysqli::next_result来设置sql的偏移量,使用mysqli::error获取当前偏移的sql的错误状态

下面是第三种方法的示例代码

代码如下:

复制代码 代码如下:


$sql = Config::get('sql');
$content = file_get_contents($sql);
$config = Config::get('config')
$mysqli = mysqli_connect($config['host'], $config['user'], $config['password'], $config['dbname']);
$ret = $mysqli->multi_query($content);
if($ret === false) {
    echo mysqli_error($mysqli);
}
while (mysqli_more_results($mysqli)) {
    if (mysqli_next_result($mysqli) === false) {
        echo mysqli_error($mysqli);
        echo "\r\n";
        break;
    }
}
$mysqli->close();


这样的话当sqls语句中任意一条有错误的话,程序就会跳出这个错误。

如果你要编写初始化mysql的脚本的话,这招就非常好用了。

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!