• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

MySQL中创建表 – PHP

php 来源:Jimmyt1988 3次浏览

问题得到这个工作:MySQL中创建表 – PHP

我把这个查询字符串(作为变量):

"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 
       )" 

到这个循环:

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 />"; 
    } 
} 

数据库:查询是在这里:

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; 
    } 

它失败闯过:

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

正在准备的字符串是在这里:

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) 

===========解决方案如下:

你应该使用str_replace

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

删除查询字符串的\r\n或者你可以做与preg_replace相同:

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

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)