0

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
1
  • 3
    You have to echo inside the loop... You are essentially echoing the initial value as many iterations as there are records. See what I mean by just echoing inside your loop to test. Commented May 25, 2013 at 14:53

4 Answers 4

2

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       
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0
$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>";
 }

1 Comment

Watch your closing short tag after $sname1
0

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
}

2 Comments

extract() is awful and error-prone. Why not just do $man['FName'];
@PhilipWhitehouse It depends on need, really. For templating systems, using extract is the easier/cleaner solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.