PHP SQL Foreach语句 [英] PHP SQL Foreach Statement

查看:85
本文介绍了PHP SQL Foreach语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解PHP一段时间了,但是还不知道foreach()命令的工作方式.我确实知道它与while()类似.我已经研究了它的工作原理,但是不确定如何将mySQL语句连接到它.

I have known PHP for a while, but have not known how the foreach() command works. I do know that it is similar to while(). I have looked around at how it works, but am not quite sure about how you would connect mySQL statements to it.

例如,您有一条如下的SQL语句:

Say for example you had an SQL statement as follows:

"SELECT username,password,email,dob FROM users"

您将如何在foreach()语句中实现此功能,以回显每个用户的用户名,密码,电子邮件和出生日期?

How would you implement this into a foreach() statement to echo every user's username, password, email and date of birth?

谢谢所有的帮助!

推荐答案

foreach方法从头到尾循环遍历数组(或对象)的所有行.这与while循环的不同之处在于while一直循环直到满足特定条件.这可以是2次迭代或1000次迭代之后,具体取决于您设置的条件.

The foreach method loops over all the rows of an array (or object), from the first until the last. This differs from a while loop in that way that a while keeps on looping until a certain condition is met. This can be after 2 iterations or 1000 iterations, depending on which condition you set.

foreach循环中,您知道迭代与数组中的键一样多(除非您在其中使用了break语句,该语句立即中止了该操作).

In a foreach loop, you know that there will be as many iterations as there are keys in the array (unless you use a break statement within it, that aborts it right away).

SQL结果也可以作为数组或对象返回,可以循环访问.例如:

SQL results can also be returned as array or object, which can be looped over. For example:

$query = "SELECT username,password,email,dob FROM users";
$resultSet = mysqli_fetch_all($query, MYSQLI_BOTH);
foreach ($resultSet as $id => $row) {
    echo $row['username'] // Show username
}

这篇关于PHP SQL Foreach语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆