Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Php PDO-对非对象调用成员函数execute()_Php_Pdo - Fatal编程技术网

Php PDO-对非对象调用成员函数execute()

Php PDO-对非对象调用成员函数execute(),php,pdo,Php,Pdo,我对PDO有问题… 类,其中我有一个错误,用于使用PHP:PDO执行对数据库的select(以及将来的其他)查询。。。就像这样: $db = new PDOAct; $arr = array("from" => "users"); $row = array("id"); $val = array(0); $type = array("INT"); $db->select($arr, $row, $val, $type);

我对PDO有问题…
类,其中我有一个错误,用于使用PHP:PDO执行对数据库的select(以及将来的其他)查询。。。就像这样:

$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");
    }
}
?>
所以$this->db->prepare()是OK。 有什么问题吗?我为此痛苦了3个多小时。

谢谢大家!

你不能做这样的事情:

 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]."'");
 }
if($i<1)
{
$do=$this->db->prepare(“选择*FROM`.”$arr[FROM].“`WHERE`.”$row[$i].“`=':”$row[$i].“”;
}否则{
$do=$do.$this->db->prepare(“和“`.”$row[$i]。“`=':“$row[$i]。””);
}

您需要(动态)生成sql语句,并且只有当您准备好完整语句时,您才能准备它,您不能连接和准备部分语句。

如果您在非对象上调用execute,请确保使用正确的顺序

$data = array("id" => "your_row_id");
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder
$STH->execute($data);

看起来里面没有execute()调用…是的。这里有var_dump(),但是如果我把$do->execute()放在这里,它会给我一个错误。除了上面的注释,你应该把
$arr[select]
改成
$arr['select']
等等。@ZLOI\u DED这是因为范围解析。好的。。。它正在工作。。。我想…=)谢谢你!好啊这就是为什么:
$db=newpdoact;$arr=数组(“来自”=>“用户”);$row=数组(“id”、“登录”);$val=数组(0,“管理”);$type=数组(“INT”、“STR”);$db->select($arr,$row,$val,$type)
不起作用?所以我需要生成sql查询并将其放入变量中,然后准备一个变量?@ZLOI\u DED是的,您需要生成一个完整的查询字符串,完成后,在执行该字符串之前对该字符串使用
prepare
。现在,您正在连接对象,除了您不能只准备
条件。谢谢你)这个错误已经纠正了。现在我有了“在非对象上调用成员函数bindValue()”,这可能是因为我在prepare()之前做的吧?代码:$bindrow=':'。$row[$i]$bindtype=“PDO::PARAM_389;”..$type[$i]$do->bindValue($bindrow,$val[$i],$bindtype);这是为了()…好的!完成了!谢谢大家)现在我还有一个问题:语句执行正常,但当我这样做smth时:
$db=newpdoact$arr=数组(“来自”=>“用户”)$行=数组(“id”、“登录”)$val=数组(0,“管理”)$类型=数组(“INT”、“STR”)$db->select($arr,$row,$val,$type)-我收到一个500内部服务器错误。。。
$data = array("id" => "your_row_id");
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder
$STH->execute($data);