'(PDO) Gather specific data from id
i Have an url stands as : http://website.com/update.php?id={NUMBER}
How would I make PDO grabs the results from that specific id?
Here is my attempt for update.php page :
try {
$dbh = new PDO("mysql:host=$hostname;dbname=vector",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = '. $id .'";
foreach ($dbh->query($sql) as $row)
{
?>
<?php echo $row['username']; ?>
<?php
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Solution 1:[1]
I assume your ID is a number so don't use = 'id'
also you should use prepared statments to protect against SQL injection attacks
$sql = "SELECT * FROM users WHERE id = :id";
$bind = array( 'id' => $id );
$sth = $dbh->prepare($sql);
$sth->execute($bind);
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
...
}
Solution 2:[2]
You need to indicate orderId instead of order_id in associations in order to use a foreign key field properly:
Order.hasMany(OrderItem, { as: 'items', foreignKey: 'orderId' });
OrderItem.belongsTo(Order, { as: 'order', foreignKey: 'orderId' });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Anatoly |
