php - pdo向数据库插入数据失败?

查看:110
本文介绍了php - pdo向数据库插入数据失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

错误代码:

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 admin (userid,password,collection) values ('John', 'Doe', 'jo')' at line 1

pdo代码:

$servername="localhost";
$user="root";
$psw="444444";
try{
            
            
            $con=new PDO("mysql:host=$servername;dbname:mydb",$user,$psw);
            $con->query("set names utf8");
            $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            
            if($con){
                
                $sql="use mydb insert into admin (userid,password,collection) values ('John', 'Doe', 'jo')";
                $con->exec($sql);

            }
            }
            catch(PDOException $e)
            {
                echo  $e->getMessage();
            }

报错信息说我sql有语法错误,但是我的sql语句在mysql执行时是可以插入数据的,而且有插入权限,请问为什么这里出错了?

解决方案

use mydb后面加个分号,因为这是两句sql

这篇关于php - pdo向数据库插入数据失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆