While循环PHP get

编程入门 行业动态 更新时间:2024-10-25 16:27:56
本文介绍了While循环PHP get_result无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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

本文发布于:2023-10-12 16:36:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!