Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以向select查询结果php pdo添加变量值_Php_Mysql_Arrays_Select_Pdo - Fatal编程技术网

是否可以向select查询结果php pdo添加变量值

是否可以向select查询结果php pdo添加变量值,php,mysql,arrays,select,pdo,Php,Mysql,Arrays,Select,Pdo,假设我有这样一个问题: $stmt = $con->_con->prepare("Select id from table", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) { $rows[] = array_map('utf8_encode', $selected_row); } 从中我得到: [{

假设我有这样一个问题:

$stmt = $con->_con->prepare("Select id from table", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $rows[] =  array_map('utf8_encode', $selected_row);
} 
从中我得到:

[{
    "id": "000060000000000071964708\/17\/201309:55:00"
}]
我想添加另一个键值对,如:

[{
    "id": "000060000000000071964708\/17\/201309:55:00",
    "column": "code1"   
}]
从结果中,我想添加一个键
,其值为
code1
,该值是动态的,我想通过select查询之外的条件来设置它。 我尝试在while循环中手动添加列,如下所示:

while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $rows[] =  array_map('utf8_encode', $selected_row);
    array_push($rows, array('column' => $column));
}
其中,
$column='code1'
,这取决于条件。但它给了我一个结果:

[{
    "id": "000060000000000071964708\/17\/201309:55:00"
}, {
    "column": "code1"
}]
这不是所需的输出。还尝试了执行
Select id,列为'code1'…
,其中我手动尝试将要返回的密钥对值作为Select查询的一部分,但失败了,并在
中给出了错误作为….

如何从示例中获得所需的输出? 我愿意接受比我心目中的想法更好的其他解决方案


您在SQL中这样做的尝试几乎是正确的,但正确的形式是

SELECT 
  id,
  'code1' AS `column`
FROM...
以带引号的字符串文字形式返回静态值,并使用列别名。因此,您可以使用静态值查询它,如下所示:

$stmt = $con->_con->prepare("Select id, 'code1' AS `column` from table", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
获取时,它将以所需格式返回结果。但是,由于该值是动态的,因此可以并且应该为其使用PDO占位符。从您使用的PDO包装器类中不清楚在何处绑定参数,但是
prepare()
调用类似于SQL

// Whatever the source of the dynamic value...
$your_dynamic_value = 'code1';
// Adds :column as a prepared statement placeholder
$stmt = $con->_con->prepare("Select id, :column AS `column` from table", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
我必须让您在这个不熟悉的PDO包装类上执行时正确调用
bindParam()
或传递带有
[':column'=>$your_dynamic_value]
的param数组,其中
prepare()
方法似乎比正常的
PDO::prepare()
做得更多

如果要在提取过程中添加它,则需要在将其添加到迭代器/fetch变量
$selected\u row
之前添加它:

while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Add an array key to the fetched row before calling array_map()
    // and appending to the $rows collection
    $selected_row['column'] = 'code1';
    $rows[] =  array_map('utf8_encode', $selected_row);
}

我想澄清哪个是最好的解决方案?第一个还是第二个?我个人更喜欢将
code1
添加到
SELECT
中,因为您将其视为查询结果的一部分。它给数据库查询增加的开销很小。向输出数组中添加值可能会使其来源变得不那么清晰,因为您稍后会回溯并忘记它来自何处。但另一方面,也许有一天有人看到数据库源表时会感到困惑,为什么该列不存在。这两种解决方案都没有明显的优越性,但我发现
SELECT
更整洁。如果需要,也可以使用它在应用程序代码之外的SQL中复制相同的查询结果。