数据库表值未输出

时间:2014-07-10 08:34:18

标签: php

   <table border="2">
                   <tr>
                      <th>Memeber Id</th>
                      <th>Lastname</th>
                      <th>Firstname</th>
                      <th>Birthdate</th>
                      <th>Gender</th>
                      <th>Status</th>
                      <th>Dedication Date</th>
                      <th>Acceptance Date</th>
                      <th>Baptism Date</th>
                      <th>Mother</th>
                      <th>Father</th>
                      <th>Decription</th>
                    </tr>    

            <?php
                  $con = mysql_connect("localhost", "root", "");
                  $er = mysql_select_db("memberdb");
                  $query = "insert into memberinformation values('$memberid','$lastname','$firstname','$birthdate','$gender','$status','$dedicationdate'
                    ,'$acceptancedate','$baptismdate','$mother','$father','$description')";
                  $result = mysql_query($query);
                  $result = mysql_query("SELECT * FROM memberinformation");

                  if (mysqli_connect_errno())
                    {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                        //you need to exit the script, if there is an error
                        exit();
                    } 

                    while ($array = mysql_fetch_row($result));
                    {
                        echo "<tr>";
                        echo "<td>" . $row['memberid'] . "</td>";
                        echo "<td>" . $row['lastname'] . "</td>";
                        echo "<td>" . $row['firstname'] . "</td>";
                        echo "<td>" . $row['birthdate'] . "</td>";
                        echo "<td>" . $row['gender'] . "</td>";
                        echo "<td>" . $row['status'] . "</td>";
                        echo "<td>" . $row['dedicationdate'] . "</td>";
                        echo "<td>" . $row['acceptancedate'] . "</td>";
                        echo "<td>" . $row['baptismdate'] . "</td>";
                        echo "<td>" . $row['mother'] . "</td>";
                        echo "<td>" . $row['father'] . "</td>";
                        echo "<td>" . $row['description'] . "</td>";
                        echo "</tr>";

                        echo "<script> alert('Tama ka.'); </script>";
                    }

                    echo "</table>";

                    mysql_close($con);
            ?>

我遇到了问题,因为代码没有输出我期望的内容。它应该从成员数据库中的memberinformation表输出数据。如果此代码中有一些缺少的行或者我有一些错误,请帮助我。

5 个答案:

答案 0 :(得分:2)

您正在读取值$ row,而数据位于$ array:

while ($array = mysql_fetch_row($result));
...
echo "<td>" . $row['memberid'] . "</td>";

这应该有效:

while ($row = mysql_fetch_row($result)){
...
echo "<td>" . $row['memberid'] . "</td>";
...
}

答案 1 :(得分:2)

你混淆了MySQL和MySQLi库。

mysqli_connect_errno()替换为mysql_errno()以使用MySQL库。 (也适用于mysqli_connect_error()

但是不要使用已弃用的MySQL库(mysql_*函数),这一点非常重要。用MySQLi和PDO替换它。

答案 2 :(得分:1)

您有$array = mysql_fetch_row($result),但在表中您正在打印,例如$ row ['memberid']而不是$array['memberid']。我相信这将是你的问题。

答案 3 :(得分:1)

将$ array = mysql_fetch_row($ result)更改为$ row = mysql_fetch_row($ result)

答案 4 :(得分:0)

  • 首先,停止使用mysql_函数
  • 其次,您正在混合使用API​​(mysql_mysqli_)。请改为选择mysqli_*
  • 第三,它应该是$row = mysql_fetch_row($result)
  • 第四,尽可能使用参数化查询/预备语句
  • 第五,始终打开错误报告

示例:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$con = new mysqli('localhost', 'root', '', 'memberdb');
$result = mysqli_query($con, 'SELECT * FROM memberinformation');
 if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    echo "<script> alert('May Mali Ka.');</script>";
    //you need to exit the script, if there is an error
    exit();
}

$stmt = $con->prepare('INSERT INTO memberinformation VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$stmt->bind_param('ssssssssssss', $memberid,$lastname,$firstname,$birthdate,$gender,$status,$dedicationdate,$acceptancedate,$baptismdate,$mother,$father,$description);
$stmt->execute();

?>
<style>table, tr, td {border: 1px solid black;}</style>
<table>
    <tr>
        <th>Memeber Id</th><th>Lastname</th><th>Firstname</th><th>Birthdate</th><th>Gender</th><th>Status</th><th>Dedication Date</th>
        <th>Acceptance Date</th><th>Baptism Date</th><th>Mother</th><th>Father</th><th>Decription</th>
    </tr>
    <?php while($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo $row['lastname']; ?></td>
            <td><?php echo $row['firstname']; ?></td>
            <td><?php echo $row['birthdate']; ?></td>
            <td><?php echo $row['gender']; ?></td>
            <td><?php echo $row['status']; ?></td>
            <td><?php echo $row['dedicationdate']; ?></td>
            <td><?php echo $row['acceptancedate']; ?></td>
            <td><?php echo $row['baptismdate']; ?></td>
            <td><?php echo $row['mother']; ?></td>
            <td><?php echo $row['father']; ?></td>
            <td><?php echo $row['description']; ?></td> 
        </tr>
    <?php endwhile; ?>
</table>
相关问题