我有一个PDO的问题...
类,其中我有一个错误,用于执行选择(和未来 - 其他)查询数据库,使用PHP:PDO ......这样的:PDO - 调用一个非对象的成员函数execute()
$db = new PDOAct;
$arr = array("from" => "users");
$row = array("id");
$val = array(0);
$type = array("INT");
$db->select($arr, $row, $val, $type);
当我执行此,
<? if (!defined("sKEY")) { exit("Houston, We've Got a Problem"); }
class PDOAct
{
public $db;
function __construct()
{
try {
$this->db = new PDO(DBdriver.':host='.DBhost.';dbname='.DBbase, DBuser, DBpass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
$this->err($e->getMessage());
}
}
function select($arr, $row, $val, $type)
{
try {
for ($i=0; $i<count($row); $i++)
{
if (isset($row[$i]) && isset($val[$i]) && isset($type[$i]))
{
if ($arr[select] != "" && $arr[select] != "*")
{
if ($i < 1)
{
$do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'");
} else {
$do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'");
}
} elseif ($arr[select] == "" || $arr[select] == "*") {
if ($i < 1)
{
$do = $this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'");
} else {
$do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'");
}
}
$do->bindValue(':'.$row[$i], $val[$i], "PDO::PARAM_".$type[$i]);
} elseif (!isset($row[$i]) && !isset($val[$i]) && !isset($type[$i])) {
if ($arr[select] != "" && $arr[select] != "*" && $i == 0)
{
$do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."`");
} elseif ($arr[select] == "" || $arr[select] == "*" && $i == 0) {
$do = $this->db->prepare("SELECT * FROM `".$arr[from]."`");
}
} else {
exit("Query error!");
}
}
var_dump($this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[0]."` = ':".$row[0]."'"));
} catch(PDOException $e) {
$this->err($e->getMessage());
}
return $do;
}
function err($e)
{
file_put_contents('log'.DIR_SEP.'PDOerrors.txt', $e."\n", FILE_APPEND);
exit("Houston, We've Got a Problem");
}
}
?>
它给了我一个PHP错误:“调用一个成员函数执行( )在非物件上“
我试图用var_dump();
- 它给了我不服这样的:
object(PDOStatement)#4 (1) { ["queryString"]=> string(40) "SELECT * FROM `users` WHERE `id` = ':id'" }
所以$这个 - > DB->准备()是OK。 什么问题?我在这3个多小时里受到了这个痛苦..
谢谢!
看起来像有没有执行()调用在那里... –
是的。有var_dump(),但是如果我在这里放置$ do-> execute(),它会给我一个错误.. –
除了上面的注释,你应该改变'$ arr [select]'到'$ arr ['select'] '等等。 – hjpotter92