从mysql转换为mysqli问题

时间:2011-03-26 14:13:13

标签: mysqli

我最近从使用mysql改为msqli,从那以后CRUD功能都停止了正常工作。请有人指出我正确的方向。这是我的代码。抱歉我的英语不好。

public function create() {
global $database;
$attributes = $this->sanitised_attributes();
$sql = "INSERT INTO ".self::$table_name."(";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {    
return false;
}

}

这是班级

class mysqliDatabase {

private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;  

    function __construct() {
    $this->open_connection();
    $this->magic_quotes_active = get_magic_quotes_gpc();
    $this->real_escape_string_exists = function_exists("mysqli_real_escape_string");
    }

    public function open_connection(){
    $this->connection = new mysqli(DB_HOST,DB_USER,DB_PASSWORD);
    if(!$this->connection) {
    die("Database connection failed: " . mysqli_error());
      } else {
    $db_select = mysqli_select_db($this->connection,'*****');
    if (!$db_select) { 
    die("Database selection failed: " . mysqli_error());    
    } else { echo "connected";}
    }
     }


    public function close_connection(){
    if(isset($this->connection)) {
    mysqli_close($this->connection);
    unset($this->connection);
  }
 }

    public function query($sql) {
    $this->last_query = $sql;
    $result = mysqli_real_query($this->connection, $sql);
    $this->confirm_query($result);
    return $result;
    }

    public function escape_value($value) {

    if($this->real_escape_string_exists) {
    //undo any magic quote effects so mysqli_real_escape can do the work
    if($this->magic_quotes_active){ $value = stripslashes($value);}
    $value = mysqli_escape_string($this->connection, $value);
    } else {
    //if magic quotes aren't already on then add slashes manually
    if(!$this->magic_quotes_active) { $value = addslashes($value);
    }
    //if magic quotes are active, then the slashes already exist
    }
    return $value;
}

    public function fetch_array($result_set) {
    return mysqli_fetch_array($result_set);
}

    public function num_rows($result_set) { 
    return mysqli_num_rows($result_set);
}

    public function    insert_id() {
    //get the last id inserted over the current db connection
    return mysqli_insert_id($this->connection);
    }

    public function affected_rows() {
    return mysqli_affected_rows($this->connection);
    }

    private function confirm_query($result) {
    if(!$result) {
    $output = "Database query failed " . mysqli_error($this->connection) . "<br
    /><br />";
    $output .= "last SQL query: " . $this->last_query;
    die($output);
 }
}

1 个答案:

答案 0 :(得分:0)

问题不在于此函数,而在于数据库类。你需要分享那个才能得到帮助。