-2

Is there something wrong with my query?

function needExport($table, $key, $id, $stamp) {
    try {
        $dbW = dbConCC();
        $stmt = $dbW->prepare("SELECT * FROM :table LIMIT 10");
        $params = array(':table' => "tmlaender");
        $stmt->execute($params);
        while ($row = $stmt->fetch()) {
            echo $row;
        }
    }
    catch(PDOException $e) {
        echo json_encode($e->getMessage());
    }
}

I get the follwoing Syntax Error:

"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tmlaender' LIMIT 1' at line 1"

Can't find any mistakes in this "simple" query.

robert
  • 797
  • 3
  • 9
  • 23

1 Answers1

0

try this:

function needExport($table, $key, $id, $stamp) {
    try {
        $dbW = dbConCC();
        $stmt = $dbW->prepare("SELECT * FROM `table`");
        $stmt->execute();

        $fetch = $stmt->fetch(PDO::FETCH_OBJ);

        return $fetch;
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}

then when echoing a field from table you'd simply:

$var1 = $var->needExport();

echo $var1->idColumn;

Note:

Binding Params is only used when using a WHERE such as:

public function getId($userid)
{
    $dbW = dbConCC();
    $stmt = $dbW->prepare("SELECT * FROM `users` WHERE `userid` = :uid");
    $stmt->bindParam(':uid', $userid);
    $stmt->execute();
}
Option
  • 2,605
  • 2
  • 19
  • 29