I am trying to make a simple search-function that's going to select and show everything, from a column.
if(isset($_GET["search"]) && ($_POST["filter"] != "all")) {
$filter = $_POST["filter"];
$sql = "SELECT :table FROM johnson LIMIT 0,30";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(":table", $filter, PDO::PARAM_STR);
$stmt->execute();
}
This snippet seems to work just as it should.
Then I'm trying to display the data:
<?php } else if($filter != "all") { ?>
<tr>
<td><?php echo $row["$filter"]; ?></td>
</tr>
<?php
}
$filter
corresponds with the proper database column, like if $filter == 'email'
, it should be SELECT email FROM johnson
.
The result? After querying the database, it just loops the column name like:
email
email
email
email
And not foo@bar.com
, etc. Whats wrong? Using PDO::FETCH_ASSOC
.
It even works when hardcoded, and not using :table
.