简体   繁体   English

如何获取我所有选择的数据并用while循环回显? 只取一条记录

[英]How to fetch all of my selected data and echo it with a while loop? Only fetching one record

$sqlWork = "SELECT * FROM work WHERE user_id = '$_SESSION[user_id]'";     
$resultWork = $connection->query($sqlWork);
$rowWork = $resultWork->fetch_assoc();

I currently have this table. 我目前有这张桌子。

(I would add an image if I had the reputation) (如果有声誉,我会添加一张图片)

http://imgur.com/fEYrzY0 http://imgur.com/fEYrzY0

This is how I am outputting my data. 这就是我输出数据的方式。

    do {         
        echo "{$rowWork['work']}";     
    } while ($description = mysqli_fetch_array($resultWork));

use: 采用:

while($rowWork = $resultWork->fetch_assoc()){
        echo $rowWork['work'];     
}

I see that you are not using parametrized query, your code is vulnerable to SQL injection attack, you can avoid it by using prepared statement (parametrized) 我看到您没有使用参数化查询,您的代码容易受到SQL注入攻击,您可以通过使用预处理语句(参数化)来避免它

$sqlWork = "SELECT * FROM work WHERE user_id = '$_SESSION[user_id]'";     
$resultWork = $connection->query($sqlWork);

while ($description = mysqli_fetch_array($resultWork)){
echo $description['work'];
}

You can also do this based on these: 您还可以根据以下步骤执行此操作:

foreach

Use when iterating through an array whose length is (or can be) unknown. 在迭代长度未知(或可以未知)的数组时使用。

for

Use when iterating through an array whose length is set, or, when you need a counter. 在遍历已设置长度的数组时使用,或者在需要计数器时使用。

while

Use when you're iterating through an array with the express purpose of finding, or triggering a certain flag. 当您遍历数组以明确查找或触发某个标志的目的时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM