-3

I have a problem with PDO...
Class, in which i have an error, used for executing select (and in the future - other) queries to database, using PHP:PDO... like that:

$db = new PDOAct;
    $arr    = array("from" => "users");
    $row    = array("id");
    $val    = array(0);
    $type   = array("INT");
    $db->select($arr, $row, $val, $type);

When i execute this,

<? 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");
    }
}
?>

it gives me a php-error: "Call to a member function execute() on a non-object"
I was trying to use var_dump(); - it gives me smth like this:

object(PDOStatement)#4 (1) { ["queryString"]=> string(40) "SELECT * FROM `users` WHERE `id` = ':id'" }

So $this->db->prepare() is OK. What's the problem? I am suffering about this over 3 hours..
THANK YOU!

ZLOI_DED
  • 21
  • 2
  • 7

2 Answers2

1

You cannot do stuff like this:

 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]."'");
 }

You need to generate your sql statement (dynamically) and only when you have the complete statement ready, you prepare it, you cannot concatenate and prepare partial statements.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Ok.. This why that: `$db = new PDOAct; $arr = array("from" => "users"); $row = array("id", "login"); $val = array(0, "Admin"); $type = array("INT", "STR"); $db->select($arr, $row, $val, $type);` is not working? – ZLOI_DED Jun 21 '13 at 22:42
  • So i need to generate sql-query and put it in variable and then prepare a variable? – ZLOI_DED Jun 21 '13 at 22:57
  • @ZLOI_DED Yes, you need to generate a complete query string and when that is done, you use `prepare` on that string right before you execute it. Now you are concatenating objects, apart from the fact that you cannot prepare just a `AND` condition. – jeroen Jun 21 '13 at 23:01
  • Ok. Thank u)) That error fixed. Now i have "Call to a member function bindValue() on a non-object") It's maybe because i do it before prepare()? Code: $bindrow = ':'.$row[$i]; $bindtype = "PDO::PARAM_".$type[$i]; $do->bindValue($bindrow, $val[$i], $bindtype); It's in for()... – ZLOI_DED Jun 21 '13 at 23:07
  • OK! It's done! Thank u all)) – ZLOI_DED Jun 22 '13 at 00:15
0

You are calling execute on a non-object, make sure you use the proper order

$data = array("id" => "your_row_id");
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder
$STH->execute($data);
Dani-san
  • 304
  • 2
  • 4
  • Now I have an other problem: Statement is executing normally, but when i do smth like this: `$db = new PDOAct; $arr = array("from" => "users"); $row = array("id", "login"); $val = array(0, "Admin"); $type = array("INT", "STR"); $db->select($arr, $row, $val, $type);` - I get an 500 Internal Server Error... – ZLOI_DED Jun 21 '13 at 22:48