Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

While Loop Not Working!

while($row = mysqli_fetch_array($res_acct)) {
  $name = $row['account_title'];
  $id =  $row['id'];
}

This is only showing one row.

7 Answers

There's a lot of instances, in your code, of PHP being used within HTML that aren't enclosed in PHP tags.

I'm not saying that's the problem but it'd probably help.

Hey Richard, I think you're reaching an error because "=" is an assignment operation and "==" is comparison. Right not you're assigning that function to the $row variable.

No, that has not seemed to work

<?php
include_once 'connect.php';
doDB();

$get_acct = "SELECT *, id, account_title FROM account_list  ORDER BY account_title DESC ";

$res_acct = mysqli_query($mysqli,$get_acct)or die(mysqli_error($mysqli));

if(mysqli_num_rows($res_acct) < 1) {
    $display_block = "<p><em>No accounts exist</em></p>";
} else {
    $display_block = <<<END_OF_TEXT
    <table>
    <tr>
    <th><h4>Account Name</h4></th>
    </tr>
END_OF_TEXT;
    while($row = mysqli_fetch_assoc($res_acct)) {
        $name = $row['account_title'];
        $id =  $row['id'];


    }

    $display_block .= <<<END_OF_TEXT
    <tr>
    <td><a href="show_account.php?account=$id">
    <strong> $name</strong>
    </a>
    &nbsp;
    <strong>$id</strong>

END_OF_TEXT;
}

    mysqli_free_result($res_acct);

    mysqli_close($mysqli);
    $display_block .= "</table>";
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Account Lists</title>
    </head>
    <body>
    <?php echo $display_block;?>
    </body>
</html>

When I put == it gives me these errors

''' Notice: Undefined variable: row in /Applications/XAMPP/xamppfiles/htdocs/hosting list/inc/list.php on line 18

Notice: Undefined variable: id in /Applications/XAMPP/xamppfiles/htdocs/hosting list/inc/list.php on line 28

Notice: Undefined variable: name in /Applications/XAMPP/xamppfiles/htdocs/hosting list/inc/list.php on line 31

Notice: Undefined variable: id in /Applications/XAMPP/xamppfiles/htdocs/hosting list/inc/list.php on line 32 Account Name

'''

Also on my other document it is not displaying anything at all:

<?php
include_once 'connect.php';
doDB();

if(!isset($_GET['account'])) {
    echo("Error");
    exit;
}


$account_id = mysqli_real_escape_string($mysqli , $_GET['account']);

$yes        = "SELECT id FROM account_list
              WHERE id = '".$account_id."'";

$res        = mysqli_query($mysqli,$yes)or die(mysqli_error($mysqli));

if(mysqli_num_rows($res) < 1){
    $display_block = "<p><em>You have selected an invalid account. Please <a href='list.php'> try again</a></em></p>";
} else {
    while($row = mysqli_fetch_array($res)){
        $title = $row['id'];
    }

    $get_account_info = "SELECT  `account_info` ,`id`, `domain`, `ip`, `cgi`, `username`, `password`, `mode`, `meg`, `ns1`, `ns2`, `ns3`, `ns4`, `package`, `feature`, `language`,
                        FROM account_info WHERE id = '".$account_id."'";
    $res_acct         = mysqli_query($mysqli,$get_account_info)or die(mysqli_error($mysqli));

    while($row1 = mysqli_fetch_assoc($res_acct)){
        $domain  = $row['domain'];
        $ip      = $row['ip'];
        $cgi     = $row['cgi'];
        $user    = $row['username'];
        $pass    = $row['passowrd'];
        $mod     = $row['mode'];
        $neg     = $row['meg'];
        $ns1     = $row['ns1'];
        $ns2     = $row['ns2'];
        $ns3     = $row['ns3'];
        $ns4     = $row['ns4'];
        $pack    = $row['package'];
        $feat    = $row['feature'];
        $lang    = $row['language'];

        echo "Hello world";
        $display_block = "
        <h1>Hello World</h1>
        <table>
        <tr>
        <thead>
        <th>Hello</th>
        </thead>
        </tr>
        <tr>
        <td>$domain</td>
        <td>$ip</td>
        <td>$cgi</td>
        <td>$user</td>
        <td>$pass</td>
        <td>$mod</td>
        <td>$meg</td>
        <td>$ns1</td>
        <td>$ns2</td>
        <td>$ns3</td>
        <td>$ns4</td>
        <td>$pack</td>
        <td>$feat</td>
        <td>$lang</td>
        <tr>";

    }
}
    mysqli_free_result($yes);
    mysqli_free_result($get_account_info);
    mysqli_close($mysqli);


?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php echo $display_block;?>

And gives me this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM account_info WHERE id = '1'' at line 2.

I have done what you said and now it has this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM account_info WHERE id = '1'' at line 2.