I want to make a function that selects every field with a specific criteria. So basically I want to click on a link and let some part of that link tell my php function what the criteria for the selection is.
I have a list of links like this:
<li><a href="category.php?category=cat1">Cat1</a></li>
<li><a href="">Cat2</a></li>
<li><a href="">Cat3</a></li>
<li><a href="">Cat4</a></li>
<li><a href="">Cat5</a></li>
The part of the link is .php?category=criteria. How can I send this information to my function and use it to select the fields?
This is the function I have:
public function get_by_category ($cat) {
$cat = $_GET['category'];
$sql = $this->db->prepare("SELECT * FROM Content WHERE category=?");
$sql->bindParam(1, $cat);
$sql->execute();
while ($row = $sql->fetch()) {
echo $row['Title'];
}
}
The result is nothing, no errors etc. it doesn’t know how to get the criteria because I don’t know how to direct the criteria to it. How can I make this work? It’s important that I could specify the criteria through the link.
Using PDO with named parameters usually works:
You can see what SQL is being executed by placing a
print $sql->fullStmtto inspect the prepared statement before it gets send to SQL.Update: in order to see PDO exceptions, you can try-catch them: