简体   繁体   中英

How to do multiple sql query in single execution

im using this php code to concat two querys:

$qry = "Create Table $this->tablename (".
                "id_user BIGINT NOT NULL AUTO_INCREMENT ,".
                "name VARCHAR( 128 ) NOT NULL ,".
                "lastname VARCHAR( 128 ) NOT NULL ,".
                "email VARCHAR( 128 ) NOT NULL ,".
                "password VARCHAR( 32 ) NOT NULL ,".
                "confirmcode VARCHAR(32) ,".
                "ident_number VARCHAR( 128 ) NOT NULL ,".
                "professional_area VARCHAR( 128 ) NOT NULL ,".
                "last_update_cv DATETIME NOT NULL ,".
                "last_login DATETIME NOT NULL ,".
                "PRIMARY KEY ( id_user )".
                ");";
$qry = $qry . "CREATE TABLE ". table_cv . 
" (id_cv BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY , 
id_user BIGINT NOT NULL, 
phone_number VARCHAR(35) NOT NULL, 
profile_text VARCHAR(500) NOT NULL);";

Any time i send the query i get a syntax error.
What is the correct way to do two queries in single execution?

If you are using PHP 5 you can use multi_query

http://www.php.net/manual/en/mysqli.multi-query.php

Use a HEREDOC :

$sql = <<<EOL
Create Table {$this->tablename} (
    id_user BIGINT NOT NULL AUTO_INCREMENT,
    name VARCHAR( 128 ) NOT NULL ,
    lastname VARCHAR( 128 ) NOT NULL ,
    email VARCHAR( 128 ) NOT NULL ,
    password VARCHAR( 32 ) NOT NULL ,
    confirmcode VARCHAR(32) ,
    ident_number VARCHAR( 128 ) NOT NULL ,
    professional_area VARCHAR( 128 ) NOT NULL ,
    last_update_cv DATETIME NOT NULL ,
    last_login DATETIME NOT NULL ,
    PRIMARY KEY ( id_user )
);
EOL;

They make it very easy to generate multi-line strings. However, note that MySQL's PHP driver does NOT allow multiple queries to be issued in a single query call as a security restriction (it eliminates a large class of SQL injection attacks).

You'll have to issue two separate queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM