请帮我纠正这段代码.
错误在线
$stmt->execute($params = [], $query);
当我用Dreamweaver打开文件时,每个“$params = []”都是错误的.
-Databasse.php-
<?php
include_once('connection.php'); //my connection is here
class Database extends connection{
public function __construct(){
parent::__construct();
}
public function getRow($params = [], $query){
try {
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function getRows($query, $params = []){
try {
$stmt = $this->datab->prepare($query);
$stmt->execute($params);
return $stmt->fetchAll();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
这是源代码database.php的一半
最佳答案 需要查看更多代码 – 您的查询字符串是什么,您尝试将哪些值添加到其中的数组?
通常,您的查询会有问号吗?或者可能有冒号的关键字:mykeyword作为值的占位符,然后您的数组保存“已准备好”并执行的值.
$query="select name from users where name=? and passwordHash=?";
$res=$connection->prepare($query);
$res->execute(array(
"myuser",
"dd02c7c2232759874e1c205587017bed"),
);
// OR
$query="select name from users where name=:name and passwordHash=:passwordHash";
$res=$connection->prepare($query);
$res->execute(array(
":name" => "myuser",
":passwordHash" => "dd02c7c2232759874e1c205587017bed"),
);
这将执行查询准备,转义等,并且“myuser”值替换第一个问号,哈希字符串替换第二个问号.