在mysqli中存储和遍历查询结果

编程入门 行业动态 更新时间:2024-10-21 09:09:18
本文介绍了在mysqli中存储和遍历查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我在phpi中使用mysqli面向对象样式的简单查询:

This is my simple query in php, using mysqli object oriented style:

$query = "SELECT name FROM usertable WHERE id = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param('i', $id); $id= $_GET['id']; $stmt->execute(); $stmt->bind_result($name); while($stmt->fetch()){ echo $name." "; } $stmt->free_result(); $stmt->close();

这很好.我获得了从select语句中检索到的名称列表.

This works fine. I obtain the list of name retrieved from the select statement.

现在,在一段时间内,我想使用$name变量作为另一个查询的参数,但是mysqli不允许这样做,因为我必须关闭第一个查询,然后调用第二个查询.

Now, inside the while I want use the $name variable as parameter for another query, but mysqli do not allow this, since I have to close the first query and then call the second query.

所以我认为我必须存储第一个查询的结果,然后遍历结果以调用新查询.

So I think I have to store the result of the first query and then iterate over the result calling a new query.

我尝试了以下操作:

$query = "SELECT name FROM usertable WHERE id = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param('i', $id); $id= $_GET['id']; $stmt->execute(); //$stmt->bind_result($name); $result = $stmt->store_result(); $stmt->free_result(); $stmt->close(); while ($row = $result->fetch_row()) { echo $row[0]." "; }

但这不起作用.永远不会到达while内的代码.

But this does not work. The code inside while is never reached.

NB :我想避免使用multi_query().

推荐答案

我已经解决了这个问题:

I have solved the problem:

$query = "SELECT name FROM usertable WHERE id = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param('i', $id); $id= $_GET['id']; $stmt->execute(); $result = $stmt->get_result(); $stmt->free_result(); $stmt->close(); while ($row = $result->fetch_array(MYSQLI_NUM)) { echo $row[0]." "; }

仅使用get_result()和fetch_array()

更多推荐

在mysqli中存储和遍历查询结果

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

发布评论

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

>www.elefans.com

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