循环mysql结果在mysql查询之外的php中 [英] loop mysql results in php outside of mysql query
本文介绍了循环mysql结果在mysql查询之外的php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的mysql查询有点问题.由于某种原因,我可以在mysql查询中回显所有关联的行,但在查询之外仅返回最后一行.这是我的代码.有什么建议吗?
i am having a bit of issue with a mysql query. for some reason i can echo all all associated rows inside of the mysql query but outside of the query it only return the last row. here is my code. any suggestions?
//Get all associated
$q=mysql_query("SELECT * FROM `ACCOUNT` WHERE ACCOUNT_ID='$act_id'");
while ($row = mysql_fetch_assoc($q)){
$act_name=$row['ACT_NAME'];
echo "$act_name<br>"; // This returns all rows fine
}
echo "$act_name<br>"; // This only return the last row. i would like to get all rows.
推荐答案
获取所有记录的唯一方法是使用 PDO 或 MySQLi .这是一个示例:
The only way that you can fetch all of the records is by using PDO or MySQLi. Here is an example:
$conn = new mysqli($hostname, $username, $password, $database);
$query = "SELECT * FROM `ACCOUNT` WHERE ACCOUNT_ID='$act_id'";
$results = $conn->query($query);
$resultArray = $results->fetch_all(MYSQLI_ASSOC);
正如@esqew所说,您需要停止使用mysql_*
函数.
As @esqew said, you need to stop using the mysql_*
functions.
这篇关于循环mysql结果在mysql查询之外的php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文