PHP PDO多语句插入失败:如何正确执行多个SQL语句?
时间:2024-12-6 21:06 作者:emer 分类: 无
解决 php 原生 pdo 多个处理语句中插入错误
问题:使用 php 原生 pdo 尝试执行两个 sql 语句插入,但收到以下报错:
error!: sqlstate[42000]: syntax error or access violation: 1064 you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'insert into content_table (title) values ('标题') not null' at line 1登录后复制
代码:
$sql= "alter table cate_table add varchar(255) not null". "insert into content_table (name) values ('标题') not null"; try { $dbh = new pdo($dsn, $user, $pass);; $dbh->exec($sql); } catch (pdoexception $e) { print "error!: " . $e->getmessage() die(); }登录后复制
原因:
立即学习“”;
php pdo 中执行多句 sql 语句时,每句 sql 语句必须以分号 (;) 作为结束符。而原始代码中,两个 sql 语句之间缺少分号。
解决方案:
在原始代码中添加分号即可,如下:
$sql= "ALTER TABLE cate_table ADD varchar(255) NOT NULL;"; "INSERT INTO content_table (name) VALUES ('标题') NOT NULL";登录后复制
以上就是PHP PDO多语句插入失败:如何正确执行多个SQL语句?的详细内容,!