-2

我有这个 PDO:

$id = 1;
$title = 'resourceName';
$url = 'resourceURL';
$result = array($title => $url);

include('../dbconnect.php');

$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare("SELECT resourceName, resourceURL FROM Resources WHERE categoryID = :id");
$stmt->bindParam(':id', $id);
$stmt->execute(array_values($result));
$row = $stmt->fetchAll();
print_r($row);

我刚刚收到此错误:警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

如何使 PDO 结果成为一个数组,其中 resourceName 是键,resourceURL 是值?

4

2 回答 2

1

您正在将不需要的参数绑定到查询。

$stmt->bindParam(':id', $id);
$stmt->execute(array_values($result)); //On this line. These parameters are 
                                         not needed

让我解释

$stmt->bindParam(':id', $id);

将 $id 的值绑定到 SQL 参数:id并再次绑定

$stmt->execute(array_values($result));

您正在绑定另一个没有任何索引的参数。

因此,您的查询需要 1 个参数,而您要发送两个参数

解决方案:使用其中之一

任何一个

$stmt->bindParam(':id', $id);

或者,直接这样

$stmt->execute(array(":id" => $id));

之后,从行中获取列并将它们转换为所需格式的新数组

$row = $stmt->fetchAll();
//Now assuming only one was returned from the database this might work
$new = array($row[0] -> resourceName => $row[0] -> resourceURL);
于 2013-05-30T17:13:26.647 回答
0

您正在混合参数绑定

$id = 1;
$title = 'resourceName';
$url = 'resourceURL';
$result = array($title => $url);

include('../dbconnect.php');

$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare("SELECT resourceName, resourceURL FROM Resources WHERE categoryID = :id");
$stmt->bindParam(':id', $id);
$stmt->execute(); //the array you put in execute will be used as parameter
$row = $stmt->fetchAll();
print_r($row);

http://www.php.net/manual/en/pdostatement.execute.php

于 2013-05-30T17:10:34.490 回答