开发者

How to do multiple sql query in single execution

开发者 https://www.devze.com 2023-04-03 00:54 出处:网络
im using this php code to concat two querys: $qry = \"Create Table $this->tablename (\". \"id_user BIGINT NOT NULL AUTO_INCREMENT ,\".

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_are开发者_运维问答a 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.


are you using multi_query? http://php.net/manual/en/mysqli.multi-query.php

0

精彩评论

暂无评论...
验证码 换一张
取 消