2014-01-17 115 views
0

我使用SQL Server作爲數據庫和PHP PDO連接,創建一個註冊頁面的時候,我在執行查詢PHP PDO SQL Server數據庫的問題

通知時出現此錯誤:數組字符串轉換在C:\ Webdev的\類\ db.php中在線36上 有創建account.PHP通知一個問題:C中的數組到字符串轉換:\ Webdev的\類\ db.php中在線36上

第36行 - $this->_query->bindValue($x, $param);

<?php 
    class DB { 
public static $instance = null; 

private  $_pdo = null, 
      $_query = null, 
      $_error = false, 
      $_results = null, 
      $_count = 0; 

private function __construct() { 
    try { 

     $this->_pdo = new PDO('sqlsrv:server=' . Config::get('sqlsrv/servername') . ';database=' . Config::get('sqlsrv/db'), Config::get('sqlsrv/username'), Config::get('sqlsrv/password')); 
    } catch(PDOExeption $e) { 
     die($e->getMessage()); 
    } 
} 

public static function getInstance() { 
    // Already an instance of this? Return, if not, create. 
    if(!isset(self::$instance)) { 
     self::$instance = new DB(); 
    } 
    return self::$instance; 
} 

public function query($sql, $params = array()) { 

    $this->_error = false; 

    if($this->_query = $this->_pdo->prepare($sql)) { 
     $x = 1; 
     if(count($params)) { 
      foreach($params as $param) { 
/* Line 36 */  $this->_query->bindValue($x, $param); 
       $x++; 
      } 
     } 

     if($this->_query->execute()) { 
      $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); 
      $this->_count = $this->_query->rowCount(); 
     } else { 
      $this->_error = true; 
     } 
    } 

    return $this; 
} 

public function get($table, $where) { 
    return $this->action('SELECT *', $table, $where); 
} 

public function delete($table, $where) { 
    return $this->action('DELETE', $table, $where); 
} 

public function action($action, $table, $where = array()) { 
    if(count($where) === 3) { 
     $operators = array('=', '>', '<', '>=', '<='); 

     $field  = $where[0]; 
     $operator = $where[1]; 
     $value  = $where[2]; 

     if(in_array($operator, $operators)) { 
      $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; 

      if(!$this->query($sql, array($value))->error()) { 
       return $this; 
      } 

     } 

     return false; 
    } 
} 

public function insert($table, $fields = array()) { 
    $keys = array_keys($fields); 
    $values = null; 
    $x  = 1; 

    foreach($fields as $value) { 
     $values .= "?"; 
     if($x < count($fields)) { 
      $values .= ', '; 
     } 
     $x++; 
    } 

    $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})"; 

    if(!$this->query($sql, $fields)->error()) { 
     return true; 
    } 

    return false; 
} 

public function update($table, $id, $fields = array()) { 
    $set = null; 
    $x  = 1; 

    foreach($fields as $name => $value) { 
     $set .= "{$name} = ?"; 
     if($x < count($fields)) { 
      $set .= ', '; 
     } 
     $x++; 
    } 

    $sql = "UPDATE users SET {$set} WHERE id = {$id}"; 

    if(!$this->query($sql, $fields)->error()) { 
     return true; 
    } 

    return false; 
} 

public function results() { 
    // Return result object 
    return $this->_results; 
} 

public function first() { 
    return $this->_results[0]; 
} 

public function count() { 
    // Return count 
    return $this->_count; 
} 

public function error() { 
    return $this->_error; 
}} 

爲什麼會這樣,我使用了相同的代碼,並有一個MySQL數據庫,它發送數據到數據庫沒有問題,爲什麼會是這樣的SQL Server?

+0

你這裏寫你自己的ORM?這是什麼代碼? – tadman

+0

它用於登錄和註冊我們的網站 – Jez

+1

您確實應該使用現有的ORM,如[Propel](http://propelorm.org/)或[Doctrine](http://www.doctrine-project.org/) )而不是重新發明輪子。自己寫這段代碼意味着要解決相同的問題,這些問題以前已經解決了上百次,但是沒有一個良好支持的模塊提供的社區代碼審查。 – tadman

回答

1

其中$param迭代即將在爲可能的數組:

if(count($params)) { 
    foreach($params as $param) { 

     if(is_array($param) || is_object($param)){ $param=''; } 

     $this->_query->bindValue($x, $param); 
     $x++; 
    } 
} 

建議進行調試public function insert()

// add a debug parameter 
public function insert($table, $fields = array(), $debug = false) { 

    $return = false; 

    if(is_array($fields) && count($fields) > 0 && $table != ''){ 

     // build SQL and debug SQL 
     $sql = "INSERT INTO '$table' ("; 
     $debug_sql = $sql; 

     // declare variables 
     $sql_fields = ''; 
     $values = ''; 
     $debug_values = ''; 

     foreach($fields as $k=>$v) { 

      // encase fields and values in quotes 
      $sql_fields.= "'$k',"; 
      $values.= "?,"; 
      $debug_values.= "'$v',"; 
     } 

     // remove trailing commas 
     $sql_fields = substr($sql_fields, 0, -1); 
     $values= substr($values, 0, -1); 
     $debug_values= substr($debug_values, 0, -1); 

     // finish SQL and debug SQL 
     $sql.= "$sql_fields) VALUES ($values)"; 
     $debug_sql.= "$sql_fields) VALUES ($debug_values)"; 

     if($debug === true) { 
      $return = $debug_sql; 
     } 
     else { 
      if(!$this->query($sql, $fields)->error()) { 
       $return = true; 
      } 
     } 
    } 

    return $return; 
} 

// now change the insert call to look like this 
die($this->_db->insert('dbo.users', $fields, true)); // <-- notice the true parameter 

/** 
* Use the output to directly run the SQL from the MSSQL admin console or whatever they call it and it will provide a much more useful error description 
*/ 
+0

謝謝,我已經添加到代碼中,並通過該部分,當它運行此代碼'公共函數創建($ fields = array()){ \t \t if(!$ this - > _ db-> insert 'dbo.users',$ fields)){ \t \t \t拋出新的異常('創建帳戶時出現問題'。 \t \t} \t}它拋出異常錯誤。插入來自我上面發佈的db.php。爲什麼會這樣呢? – Jez

+0

我打算假設'public function insert(){}'生成的SQL存在問題。老實說,你的新問題讓我懷疑你之前是否曾經使用過PHP,或者一般SQL ......無論哪種方式,現在都是離題,因爲你原來的問題已經解決了。 – MonkeyZeus

+0

你做了哪些修改?因爲它與我最初發布的相同。是的,看起來'公共函數insert(){}'是問題。我的問題是基於這種方式,因爲我以前從未使用過PDO,之前我已經使用過標準的PHP,而不是以這種方式使用函數或數組!我從教程中瞭解到,所有這些代碼都被髮送到一個mysql數據庫。 這一切都完美的使用時,並插入記錄應該是完美的,我知道這是我測試它。我的實際數據庫在MS SQL Server中,試圖理解爲什麼它適用於一個而不是另一個。 – Jez