错误代码:
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你在连接的时候就已经指定了数据库了,所以不需要
use mydb
。是不是符号没用好,试下这样行不行:
$sql='use mydb insert into admin ("userid","password","collection") values ("John", "Doe", "jo")';
use mydb 不需要每次都指定的,只要指定一次,然后SQL