On my php server side, the code below works fine.
$sql = 'select make, model from cars';
$stmt = $pdo->query($sql);
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
echo json_encode($row);
Here's what the front-end saw in the console (records php fetched from database): front-end console
But then when I tried to make the sql statements more flexible by using variables, I met with troubles. Here is the code. Neither this one:
$sql1 = "select make, model from :tb";
$stmt = $pdo->prepare($sql1);
$stmt->execute(array('tb' => 'cars'));
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
echo json_encode($row);
Nor this one worked:
$sql2 = "select make, model from ?";
$stmt = $pdo->prepare($sql2);
$stmt->execute(array('cars'));
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
echo json_encode($row);
I guess something was wrong with the execute statement, but could not figure this out. What did I miss here?