简体   繁体   中英

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

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)

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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