I'm trying to create a database with users and books and want to echo only the 'name' out of the query results and not the 'user_id' from the SELECT clause. Then the user can click on this name and will redirect to another page with the user_id of the SELECT result. Right now it echoes both and doesn't pass the user_id in the a href. I think it has something to do with the 'fetchAll() as $k=>$f' part but can't find the solution to this. I used an PHP tutorial example I found online. I'm very new to PHP and SQL but I really would like to learn how to fix the issue. I hope my question is written understandable. I don't know how to make it any more understandable. Thank you in advance!
Priscilla
Code:
try {
$stmt = $con->prepare("SELECT name, user_id FROM users");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo "<br>";
echo "<a href='../books/index.php?userid=?'>$v</a>";
}
}
答案 0 :(得分:-1)
您可以使用此代码,这可能会有所帮助:
try {
$stmt = $con->prepare("SELECT name, user_id FROM users");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo "<br>";
echo "<a href='../books/index.php?{$v['name']}'>{$v['id']}</a>";
}
}