简体   繁体   English

PHP PDO-将数组用于SELECT SQL语句

[英]PHP PDO - Using an array for a SELECT SQL Statement

I'm having a bit of an issue. 我有一个问题。 Basically what I am trying to do is the following; 基本上我想做的是以下内容;

  • I am using PDO 我正在使用PDO
  • I want the ability to pass an array containing the column name in the db (the key) and the info I want to insert (value). 我希望能够传递包含db中的列名(键)和要插入的信息(值)的数组。 The array may contain 1, or many fields that need to be updated. 该数组可能包含1,或者需要更新许多字段。
  • I have designed a function that looks like this: 我设计了一个看起来像这样的函数:

Sample Array being passed: 传递的样本数组:

$columnsToRetrieve = array('column1' => 'info', 
                            'column2' => 'data', 
                            'column3' => 'data');

And a sample function (did not include the DB initialization part) 和示例函数(不包括数据库初始化部分)

function updateInfo ($columnsToRetrieve, $whereClauseValue) {

    $counter = 1;
    $queryString = 'UPDATE table SET ';
    foreach ($columnsToRetrieve as $k => $V) {
        $queryString .= $k . ' = ?';    
    }
    $queryString .= 'WHERE column4 = ?'
    $stmt = $dbc->dbConnection->prepare($queryString);
    foreach ($columnsToRetrieve as $k => $v) {
        $stmt->bindParam($counter, $v);
        $counter++;
    }
    $stmt->bindParam($counter, $whereClauseValue);
    $stmt->execute();
}

The problem is with the second foreach, where I am trying to use the bindParam($counter, $value). 问题出在第二个foreach上,我在这里尝试使用bindParam($ counter,$ value)。 Although the right number and value populates, it doesn't seem to want to accept it. 尽管填充了正确的数字和值,但它似乎不想接受它。

Does anyone have any ideas how this can be done, or what I'm doing wrong above? 有谁知道如何做到这一点,或者上面我做错了什么?

After debugging your code I see two issues: 调试代码后,我看到两个问题:

  1. missing semicolon after $queryString .= 'WHERE column4 = ?' $queryString .= 'WHERE column4 = ?'后缺少分号$queryString .= 'WHERE column4 = ?' part. 部分。
  2. If I dump your resulting query, it would be something like 如果我转储您的结果查询,它将类似于

UPDATE table SET column1 = ?column2 = ?column3 = ?WHERE column4 = ?

see missing spaces. 查看缺少的空格。 So what if you modify this line: 那么,如果您修改此行:

$queryString .= $k . ' = ?';

to

$queryString .= $k . ' = ?,';

and (in order to strip the last comma) 和(以去除最后一个逗号)

 $queryString .= ' WHERE column4 = ?'

replace with 用。。。来代替

$queryString = substr($queryString,0,-1) .  ' WHERE column4 = ?';

95% sure that your issue is your use of bindParam . 95%确定您的问题是您对bindParam的使用。 bindParam works by reference, not by value. bindParam通过引用而不是按值工作。 As a result your foreach loop isn't binding your values to your query, but rather the $v variable. 结果,您的foreach循环不会将您的绑定到查询,而是将$v变量绑定。 By the time you call execute all parameters are bound to the same $v variable, so when you execute your query does not behave as expected. 在您调用execute时,所有参数都绑定到相同的$v变量,因此在执行查询时,查询的行为将不符合预期。

The solution is simple: use bindValue() or execute() . 解决方案很简单:使用bindValue()execute() Personally, I use execute() exclusively: it is simple to understand and read. 就我个人而言,我专门使用execute() :它易于理解和阅读。 Without a doubt though, the reference nature of bindParam() will cause plenty of trouble: I've been burned by it before. 但是毫无疑问, bindParam()的引用性质会引起很多麻烦:之前我已经被它烧死了。

Edit to add specific example 编辑以添加特定示例

I've copied and pasted your above code, so any errors will remain here, but it is actually much simpler with execute() 我已经复制并粘贴了上面的代码,所以任何错误都将保留在这里,但是使用execute()实际上要简单得多

function updateInfo ($columnsToRetrieve, $whereClauseValue) {

    $counter = 1;
    $queryString = 'UPDATE table SET ';
    foreach ($columnsToRetrieve as $k => $V) {
        $queryString .= $k . ' = ?';    
    }
    $queryString .= 'WHERE column4 = ?'
    $stmt = $dbc->dbConnection->prepare($queryString);

    $values = array_values( $columnsToRetrieve );
    $values[] = $whereClauseValue();

    $stmt->execute( $values );
}

With the assistance of @RiggsFolly and @Sebastian Brosch, I was able to better understand the scenario. 在@RiggsFolly和@Sebastian Brosch的帮助下,我能够更好地理解这种情况。 I looked up some past posts as well, and was able to tailor a possible solution: 我也查看了一些过去的帖子,并且能够定制一个可能的解决方案:

$columnsToRetrieve = array('name' => 'Frank', 'phone' => '1112223333');
$email = 't@t.com';

$values = array();

$string = 'UPDATE users SET';

foreach ($columnsToRetrieve as $k => $v) {
    $string .= ' ' . $k . ' = :' . $k . ',';
    $values[':' . $k] = $v;
};

$string = substr($string, 0, -1);

$string .= ' WHERE email = :email;';

$values[':email'] = $email;

try {
    $stmt = $dbc->prepare($string);
    $stmt->execute($values);
} catch (PDOException $e) {
    echo $e->getMessage();
}

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

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