简体   繁体   English

从MySQL数据库检索数据

[英]Retrieving data from MySQL database

Not sure what I'm doing here. 不知道我在这里做什么。

The following code outputs the Database connected statement, but is not displaying any records ("0 results"): 以下代码输出数据库连接的语句,但不显示任何记录(“ 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();  

 ?>

this looks like a whole lot of copy & paste from tutorials. 这看起来像是教程中的大量复制和粘贴。

The use of both mysql_ and mysqli_ functions is pretty much useless all together.. mysql_mysqli_函数的使用几乎完全没有用。

As it look slike you want it in mysql_ ive re written your code to fit your needs. 看起来很像,您希望在mysql_ ive中重新编写代码以适合您的需求。

Code: 码:

<?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(); 

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM