Question

This returns an empty array:

$query_string = "select * from :table where id=1";
$args = array(":table" => "pages");
$result = $db->Query($query_string, $args);

This pulls and returns data from database:

$query_string = "select * from pages where id=1";
$result = $db->Query($query_string);

Query method:

public function Query($query, $arguments = null) {
    $pdo_query = $this->Connection->prepare($query);
    $pdo_query->execute($arguments);
    if($pdo_query) {
        return $pdo_query->fetchAll();
    }
}

I went through samples and documentation - my syntax seems to be fine. Why does the first sample of code return empty array?

Thanks!

Was it helpful?

Solution

You cannot bind a table name to a PDO parameter. Parameters only work for operands of comparison or assignments.

OTHER TIPS

As said above you cannot bind the table name to a parameter, you should change your code like so

$query_string = "select * from pages where id=:id";
$args = array(":id" => "1"); //the : is not necessary but as it is a non-documented feature I would always set it
$result = $db->Query($query_string, $args);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top