ber2g
ber2g

Reputation: 15

Php code results not matching mySQL query

mySQL Table

ID | Name

1 | A

2 | B

3 | C

4 | D

5 | E

mySQL Query

$query = "SELECT * FROM ego_work WHERE 1;
$result = mysql_query($query);

$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}

php Code

<?php foreach ($rows as $work): ?>
     <span> <?php echo $work['id']; ?>, </span>
<?php endforeach; ?>
<br \>
<?php foreach ($rows as $work): ?>
     <span> <?php echo $work['name']; ?>, </span>
<?php endforeach; ?>

RESULT

1,2,3,4,5

E,A,B,C,D

What did I do wrong? I'm trying to get the 2nd result to be A,B,C,D,E

Upvotes: 1

Views: 121

Answers (1)

Robin Michael Poothurai
Robin Michael Poothurai

Reputation: 5664

can you try below code

<?php foreach ($rows as $work): ?>
<span> <?php echo $work['id']; ?>, </span>
<?php endforeach; ?>

<?php reset($rows);?>
<br \>
<?php foreach ($rows as $work): ?>
 <span> <?php echo $work['name']; ?>, </span>
<?php endforeach; ?>

Upvotes: 1

Related Questions