錯誤代碼:
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執行時是可以插入資料的,而且有插入權限,請問為什麼這裡出錯了?
迷茫2017-05-16 13:00:00
是不是符號沒用好,試下這樣行不行:
$sql='use mydb insert into admin ("userid","password","collection") values ("John", "Doe", "jo" )';