php - pdo fails to insert data into database?
某草草
某草草 2017-05-16 12:58:00
0
4
815

Error code:

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 code:

$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();
            }

The error message says that my sql has a syntax error, but my sql statement can insert data when mysql is executed, and I have the insert permission. Why is there an error here?

某草草
某草草

reply all(4)
曾经蜡笔没有小新

use mydbAdd a semicolon after it, because these are two sentences of sql

阿神

You have already specified the database when connecting, so there is no needuse mydb.

迷茫

Is it because the symbols are not used well? Try this:
$sql='use mydb insert into admin ("userid","password","collection") values ​​("John", "Doe", "jo" )';

小葫芦

use mydb does not need to be specified every time, just specify it once, and then SQL

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template