将mysqli_fetch_array中的项目回显到未显示的html表格标签中

时间:2014-01-10 20:21:42

标签: php mysql mysqli echo

这是我的代码,我得到了要显示的项目,但没有任何内容会在标签中回显。我试着删除php echo语句,只是尝试写一些文本,但仍然没有。提前致谢

    <?php
//Create a connection
$connect = mysqli_connect('localhost', 'root', 'bachi619', 'company');

//check connection
if(mysqli_connect_errno($connect)){
    echo 'Failed to connecto to database'.mysqli_connect_error();
}

$result= mysqli_query($connect, "SELECT * FROM employees");
?>

<br>
    <table width="500", cellpadding=5 callspacing=5 border=1>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Last Name</th>
            <th>Department</th>
            <th>Email</th>
        </tr>
    <?php while($rows = mysqli_fetch_array($result)): ?>
    <tr>
        <td><?php echo $rows['id']; ?></td>
        <td><?php echo $rows['first_name']; ?></td>
        <td><?php echo $rows['last_name']; ?></td>
        <td><?php echo $rows['department']; ?></td>
        <td><?php echo $rows['email']; ?></td>
    </tr>
    <?php endwhile; ?>



    </table>

2 个答案:

答案 0 :(得分:0)

你在几个地方错过了PHP:

<?php
$connect = mysqli_connect('localhost', 'root', '11111', 'company');
if(mysqli_connect_errno($connect)){
echo 'Failed to connecto to database'.mysqli_connect_error();}


$result= mysqli_query($connect, "SELECT * FROM employees");
?>
 <table width="500", cellpadding=5 callspacing=5 border=1>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Last Name</th>
        <th>Department</th>
        <th>Email</th>
    </tr>
<?php while($rows = mysqli_fetch_array($result)): ?>
<tr>
    <td><?php echo $rows['id']; ?></td>
    <td><?php echo $rows['first_name']; ?></td>
    <td><?php echo $rows['last_name']; ?></td>
    <td><?php echo $rows['department']; ?></td>
    <td><?php echo $rows['email']; ?></td>
</tr>
<?php endwhile; ?>



</table>

答案 1 :(得分:0)

我曾经遇到过同样的问题。起初我以为mysqli_fetch_array()无法在标签内部工作。但现在它终于做到了。无论如何,在检查您的代码后,您尝试删除:<?php while($rows = mysqli_fetch_array($result)): ?> 因为据我所知,你不需要;或者a:如果你在while-loop或任何loop function内调用某个功能。 试试这个: while($row=mysqli_fetch_assoc($result))