user1854914
user1854914

Reputation: 137

PHP Print Rows in an array

I want to echo out multiple lines in the array but it only outputs one line: PHP

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
   while ($row1 = mysql_fetch_array($result)) {
     $fname1 = $row1['FName'];       
     $sname1 = $row1['SName'];
     }

$result2 = mysql_query($query);
   while ($row2 = mysql_fetch_array($result2)) {
     $fname2 = $row2['FName'];       
     $sname2 = $row2['SName'];
     }

HTML

<h2>First Name: <?php echo "$fname1"; ?></h2>
<h2>Second Name: <?php echo "$sname1"; ?></h2>

<h2>First Name: <?php echo "$fname2"; ?></h2>
<h2>Second Name: <?php echo "$sname2"; ?></h2>

but it gives me the same output when both should be different. The output is:

First Name: John Second Name: Smith

First Name: John Second Name: Smith

When i want the output to be:

First Name: John Second Name: Smith

First Name: Bob Second Name: Marley

Can anyone help me to fix this problem please?\

The data in the database is:

User_ID| FName |SName| Category
1        John   Smith  Men
2        Bob    Marley Men

Upvotes: 0

Views: 2328

Answers (4)

cwallenpoole
cwallenpoole

Reputation: 82008

You're overwriting your values because your while loop is iterating over every row every time. Try this:

$query = "SELECT * FROM user WHERE Category = 'Men'";
// mysql_ = bad. mysqli_ = good!
$result = mysql_query($query);
$row1 = mysql_fetch_array($result);
$fname1 = $row1['FName'];
$sname1 = $row1['SName'];

// using the same $result.
$row2 = mysql_fetch_array($result);
$fname2 = $row2['FName'];       
$sname2 = $row2['SName'];

Of course, as has been stated elsewhere, if you have more than two items in your table and want each item in your output, this solution won't work. If that is the case, you'll want something like this:

$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
?>  <h2>First Name: <?=$row['FName'] ?></h2>
    <h2>Second Name: <?=$row['SName'] ?></h2><br /><?php
}

Or, depending on your needs:

$result = mysql_query($query);
$men = array();
while($row = mysql_fetch_array($result))
{
    $men[] = $row;
}

// then later in the script
foreach($men as $man)
{
    // extract takes all of the array keys and turns them into local variables.
    // just make sure you read the warnings in the docs:
    // http://php.net/manual/en/function.extract.php
    extract($man);
    ?>  
    <h2>First Name: <?=$FName ?></h2>
    <h2>Second Name: <?=$SName ?></h2><br /><?php
}

Upvotes: 0

Sina R.
Sina R.

Reputation: 1788

use arrays and also just trace your code.

PHP:

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
   $fname=array();
   $sname=array();

   for($i=0;$i<2 && ($row = mysql_fetch_array($result));$i++) {
     $fname[$i] = $row['FName'];       
     $sname[$i] = $row['SName'];
     }

HTML:

<h2>First Name: <?php echo "$fname[0]"; ?></h2>
<h2>Second Name: <?php echo "$sname[0]"; ?></h2>

<h2>First Name: <?php echo "$fname[1]"; ?></h2>
<h2>Second Name: <?php echo "$sname[1]"; ?></h2>

Upvotes: 1

Roberto Arosemena
Roberto Arosemena

Reputation: 1140

the while loop is overwriting you variable over and over and only outputting the last result, you have to either put the echo inside the while or save it to an array and then loop through the array

also obligatory "stop using mysql_ functions" and change to PDO :)

solution to your code would be

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
while ($row1 = mysql_fetch_array($result)) {
?> <h2>First Name: <?php echo $row1['FName']; ?></h2>
  <h2>Second Name: <?php echo $row1['SName']; ?></h2><?php       
}

Upvotes: 2

Srikanth Kolli
Srikanth Kolli

Reputation: 912

$query = "SELECT * FROM user WHERE Category = 'Men'";
$result = mysql_query($query);
while ($row1 = mysql_fetch_array($result)) {
 $fname1 = $row1['FName'];       
 $sname1 = $row1['SName'];
echo " <h2>First Name: $fname1</h2>";
echo "<h2>Second Name: $sname1</h2>";
 }

Upvotes: 0

Related Questions