简体   繁体   中英

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"):

<?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..

As it look slike you want it in mysql_ ive re written your code to fit your needs.

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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