从MySQL数据库中检索数据

时间:2015-10-21 05:01:09

标签: php mysql

不确定我在这做什么。

以下代码输出Database connected语句,但不显示任何记录(" 0结果"):

<?php 
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";

//Connection
$dbc = mysql_pconnect($host,$user,$password);

//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);

if (!$dbc)
    {
        die("Connection Failed: " .mysqli_connect_error());
    }
echo "Connected";


$sql = "select * from staff";
$result = $dbc -> query($sql);

if ($result ->num_rows >0 )
    {
        echo"<table><tr><th>Email</th><th>Name</th><th>Mobile</th>    <th>Address</th><th>Password</th></tr>";

        while($row = $result-> fetch_assoc())
        {
            echo "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
        }
        echo "</table>";
     }
     else
       {
        echo "0 Results";
     }
    $dbc->close();  

 ?>

1 个答案:

答案 0 :(得分:1)

这看起来像是一大堆副本&amp;从教程粘贴。

同时使用mysql_mysqli_函数几乎没用..

因为它看起来很像你想要它mysql_我已经编写了你的​​代码以满足你的需求。

代码:

<?php 
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";

//Connection 
mysql_connect($host,$user,$password) or die("An error occured while connecting...");

//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);




$sql = "select * from staff";
$Query = mysql_query($sql);

if (mysql_num_rows($Query)){


    $html .= "<table><tr><th>Email</th><th>Name</th><th>Mobile</th>    <th>Address</th><th>Password</th></tr>";

    while($row = mysql_fetch_array($Query)){

        $html .= "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
    }

    $html .= "</table>";
}

else{
    $html .= "0 Results";
}

echo $html;

mysql_close(); 

?>