我正在尝试使用MySQl准备好的语句从数据库中获取行并获取结果.但是,这是行不通的.
请有人能看到我要去哪里了吗?我已经尝试了数小时的解决方案,但无法正常工作.该页面只是不会加载,好像查询失败了一样.
$tag = trim($_GET['tag']); $stmt = $mysqli->prepare('SELECT posts.* FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?'); $stmt->bind_param('s', $tag); $stmt->execute(); $stmt->store_result(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo $row['tag']; } $stmt->free_result(); $stmt->close();解决方案
尝试一下:
$stmt = $mysqli->prepare('SELECT posts.id FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?'); ... $stmt->bind_result($id); while ($stmt->fetch()) { // var_dump entire row to ensure the key you expect is avail var_dump($id); }更新
如果您要进行选择*,而不必单独指定每个列,请查看此帖子(不是可接受的答案,而是得分最高的答案).否则,我强烈建议您检查 PDO ,因为它使这些基本的阅读操作变得更加容易.
I am trying to get rows from the database using MySQl prepared statements and get result. However this is not working.
Please can someone see where I am going wrong? I have been trying solutions for hours but I can't get it to work. The page just doesn't load as if the query has failed.
$tag = trim($_GET['tag']); $stmt = $mysqli->prepare('SELECT posts.* FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?'); $stmt->bind_param('s', $tag); $stmt->execute(); $stmt->store_result(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo $row['tag']; } $stmt->free_result(); $stmt->close();解决方案
Try this:
$stmt = $mysqli->prepare('SELECT posts.id FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?'); ... $stmt->bind_result($id); while ($stmt->fetch()) { // var_dump entire row to ensure the key you expect is avail var_dump($id); }Upate
If you want to do a select *, vs having to specify EVERY column individually, check out this post (not the accepted answer, but the highest scoring answer). Otherwise I strongly urge you to check out PDO, as it makes these basic read ops much easier.
更多推荐
While循环PHP get
发布评论