简体   繁体   English

mysql创建表-PHP

[英]mysql creating table - PHP

Problem getting this to work: 使此工作正常的问题:

I am putting this query string (as a variable): 我把这个查询字符串(作为变量):

"query" => 
            "CREATE TABLE 
                users(
                    id INT NOT NULL AUTO_INCREMENT  PRIMARY KEY,
                    date_created DATETIME,     
                    last_active DATETIME,
                    last_logged_in DATETIME,
                    first_name VARCHAR(255),
                    last_name VARCHAR(255),
                    email VARCHAR(255), 
                    password VARCHAR(255),  
                    permissions INT,  
                    status SMALLINT
                )"  

into this loop: 进入此循环:

foreach( $queryStrings as $queryString )
{  
    $query = Database::Query( $queryString[ "query" ] );
    if( $query )
    {
        echo "Database table " . $queryString[ "name" ] . " successfully created<br />"; 
    }
    else
    {     
        echo "Database table " . $queryString[ "name" ] . " failed to create<br />"; 
    }
}

The Database:Query is here: 数据库:查询在这里:

    public static function Query( $query )
    {
        $query = self::$mysqli->real_escape_string( trim( $query ) );  

        if ( $query = self::$mysqli->prepare($query) ) 
        {
            $query->execute();   

            $DatabaseQuery = new DatabaseQuery();
                $DatabaseQuery->result = $query->get_result();       
                $DatabaseQuery->mysql_num_rows = $query->num_rows();                 
                $query->close();
                return $DatabaseQuery;                   
        }          

        echo false;
    }

It fails to get past: 它无法超越:

if ( $query = self::$mysqli->prepare($query) ) 

The string it is preparing is here: 它正在准备的字符串在这里:

CREATE TABLE \r\n users(\r\n id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,\r\n date_created DATETIME, \r\n last_active DATETIME,\r\n last_logged_in DATETIME,\r\n first_name VARCHAR(255),\r\n last_name VARCHAR(255),\r\n email VARCHAR(255), \r\n password VARCHAR(255), \r\n permissions INT, \r\n status SMALLINT\r\n )

You should remove the \\r and \\n from the query string using str_replace 您应该使用str_replace从查询字符串中删除\\r\\n

 $string = str_replace(array("\n", "\r"), '', $string);

Or you could do the same with preg_replace : 或者您可以使用preg_replace进行相同的操作:

$string = preg_replace('/[\r\n]/', '', $string);

Of course removing linefeed characters is a wrong answer. 当然,删除换行符是错误的答案。

It's whole $query = self::$mysqli->real_escape_string( trim( $query ) ); 整个$query = self::$mysqli->real_escape_string( trim( $query ) ); line should be removed instead, as it's shouldn't be used on the whole query in general and practically useless for the prepared queries like this one. 相反,应删除行,因为通常不应该在整个查询中使用该行,并且对于像这样的已准备好的查询几乎没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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