PHP PDO SQL Server 数据库问题

标签 php mysql sql pdo sql-server-2008-r2

我使用 SQL Server 作为数据库并使用 PHP PDO 进行连接,创建注册页面时,执行查询时出现此错误

Notice: Array to string conversion in C:\webdev\classes\DB.php on line 36 There was a problem creating an account.PHP Notice: Array to string conversion in C:\webdev\classes\DB.php on line 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 会出现这种情况?

最佳答案

其中一个 $param 迭代可能以数组形式出现:

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

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

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

调试建议公共(public)函数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
 */

关于PHP PDO SQL Server 数据库问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21195483/

相关文章:

php - Android:发送要存储在MySQL中的数据

mysql - 在 MySQL 中计算组合 ID,然后将其存储为库存

php - Facebook 应用程序成功发布到页面,但非开发人员看不到帖子。我真的必须审查只有 1 个用户的应用程序吗?

javascript - 如何从另一台服务器接收数据(如推送通知)

MySQL - 如果其他数据库上不存在数据代码失败

mysql - 具有两个连接表的表的主键/外键

mysql - 限制一张表的SQL连接

jquery - SQL 查询错误(NOT IN 运算符)

java - 如何跟踪或至少检测无效的电子邮件地址

php - 我在加载时有一个页面,通过 SQL 查询加载图像表,我想更改该表在同一页面上的工作方式