简体   繁体   中英

Displaying MySQL table in PHP

I have a quick question. I have the while statement below that is working but is printing everything in the table twice. I'm new at PHP and MySQL and don't have any idea why. Can someone help point me in the right direction?

while($row = mysql_fetch_array($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
     foreach($row as $cell)
        echo "<td>$cell</td>";
        $linkID= $row['linkID'];
    echo '<td><a href="update.php?linkID=' . $linkID. '">Update Status</a></td>';

    echo "</tr>\n";
}

your code is correct, the only problem is that mysql_fetch_array take a second parameter, and there are three options (MYSQL_BOTH, MYSQL_ASSOC, MYSQL_NUM) for this second parameter.

by default it is set at MYSQL_BOTH if you don't specify it , who give you two arrays , one with numeric indices and other with associative indices. and this is the source of your problem

you can simply fixe that by adding this param as :

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
.....

MYSQL_NUM also work if you want.

Note :

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used

for this reason you must to use pdo extension, this is better and you will appreciate it.

$row = mysql_fetch_array($result);
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
     foreach($row as $cell)
        echo "<td>$cell</td>";
        $linkID= $row['linkID'];
    echo '<td><a href="update.php?linkID=' . $linkID. '">Update Status</a></td>';

    echo "</tr>\n";

try this

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