显示MySQL数据库中的记录

时间:2014-02-06 02:00:49

标签: php sql

我有这个PHP页面,应该是一个有职位的员工列表..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"         "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
    // connect to the database
    include('connection.php');
?>
<?php
    // get results from database
    $result = "SELECT employees.id, CONCAT( fname, lname ) AS FullName,     employees.hphone, employees.cphone, employees.email, position.pos\n"
. "FROM employees\n"
. "INNER JOIN position ON employees.posid = position.id\n"
. "ORDER by employees.id ASC LIMIT 0, 30 ";     
        or die(mysql_error());  

    // display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {

            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['employees.id'] . '</td>'
            echo '<td>' . $row['FullName'] . '</td>';
            echo '<td>' . $row['employees.hphone'] . '</td>';
            echo '<td>' . $row['employees.cphone'] . '</td>';
            echo '<td>' . $row['employees.email'] . '</td>';
            echo '<td>' . $row['position.pos'] . '</td>';                
            echo '<td><a href="edit.php?id=' . $row['employees.id'] . '">Edit</a></td>';
            echo '<td><a href="delete.php?id=' . $row['employees.id'] . '">Delete</a>    </td>';
            echo "</tr>"; 
    } 

    // close table>
    echo "</table>";
?>
<p><a href="drop.php">Add a new record</a></p>
</body>
</html> 

但我只能得到一个白页..没有错误没有什么....有人可以帮助我..我正在使用Linux mysql和PHP ....我知道sql工作,因为我可以得到记录从它通过MyPHPAdmin ..

请帮忙。

2 个答案:

答案 0 :(得分:0)

试试这个:

$con=mysqli_connect("example.com","peter","abc123","my_db");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

    $sql = "SELECT employees.id, CONCAT( fname, lname ) AS FullName,     employees.hphone, employees.cphone, employees.email, position.pos\n"
    . "FROM employees\n"
    . "INNER JOIN position ON employees.posid = position.id\n"
    . "ORDER by employees.id ASC LIMIT 0, 30 ";     



    $result = mysqli_query($con,$sql);




// display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>ID</th> <th>Employee Name/th> <th>Home Phone</th> <th>Cell Phone</th> <th>Email</th> <th>Position</th> </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysqli_fetch_array($result)) {

            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['employees.id'] . '</td>';
            echo '<td>' . $row['FullName'] . '</td>';
            echo '<td>' . $row['employees.hphone'] . '</td>';
            echo '<td>' . $row['employees.cphone'] . '</td>';
            echo '<td>' . $row['employees.email'] . '</td>';
            echo '<td>' . $row['position.pos'] . '</td>';                
            echo '<td><a href="edit.php?id=' . $row['employees.id'] . '">Edit</a></td>';
            echo '<td><a href="delete.php?id=' . $row['employees.id'] . '">Delete</a>    </td>';
            echo "</tr>"; 
    } 

    // close table>
    echo "</table>";

答案 1 :(得分:0)

乍一看,您没有运行$result = mysqli_query($conn, $some_query_string_here)并在字符串声明上调用or die()。由于我们无法看到您的connection.php文件中的内容,我们看不到您在那里做了什么,但问题是您没有对SQL字符串执行查询并将其分配给结果,您只需指定字符串结果。

要在将来进行问题排查,您应该在服务器上启用错误:

  1. 在您的计算机/服务器上找到php.ini文件并按如下所示进行设置:

    error_reporting = E_ALL&amp; ~E_NOTICE | E_STRICT

    display_errors = On

  2. 重启您的网络服务器(Apache或Nginx或PHP-FPM,具体取决于您的设置方式)

  3. 查看屏幕上的错误并一次修复一次,直至其有效。


  4. 启用错误报告的替代方法是脚本目录中的.htaccess文件或具有以下php_value error_reporting 0的父目录


    如果您无法查看错误,请在代码中注释掉php行,保存,然后刷新浏览器直到页面呈现。你注释掉的最后一件事通常是罪魁祸首然后调试。