无法从数据库

时间:2017-04-23 00:43:50

标签: php html

我的div无法从数据库加载内容。 这是我的代码:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "login";


  $conn = mysqli_connect($servername, $username, $password, $dbname);

  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }


  $sql = "SELECT id, product, quantity FROM products";
  $result = mysqli_query($conn, $sql);
  // output data of each row
   if (mysqli_num_rows ($result) > 0) {

  while($row = mysqli_fetch_assoc($result)) {

  }
  } 
  else {
  echo "0 results";
  }

  mysqli_close($conn);
  ?> 
<!DOCTYPE html>
<html>
  <head>
    <title>page</title>
    <link rel="stylesheet" href="page.css"
      type="text/css">
  </head>
  <body>
    <div id="wrapper">
      <div id="head"></div>
      <br>
      <div id="navi"></div>
      <br>
      <div id="left-column"><?php          
        echo $row['product']; 
        ?>
      </div>
      <div id="center-column"></div>
      <div id="right-column"></div>
    </div>
  </body>
</html>

3 个答案:

答案 0 :(得分:2)

这是php部分 *将您的SQL查询分配给$query *为mysqli_connect()

提供正确的数据库详细信息
<?php

$conn =mysqli_connect('host', 'username', 'password', 'database');

$query = ""; // your sql query

$result = mysqli_query($conn, $query);

 while($row = mysqli_fetch_assoc($result))
 {
     $product = $row['product'];
 }

?>

这是你的html部分

<!DOCTYPE html>
<html>
<head><title>page</title>
<link rel="stylesheet" href="page.css"
    type="text/css"> </head>
<body>
<div id="wrapper">
<div id="head"></div><br>
<div id="navi"></div><br>
<div id="left-column"><?php          
     if(isset($product)){echo $product; }
    ?>
    </div>
<div id="center-column"></div>
<div id="right-column"></div>
   </div> 
</body>
</html>

答案 1 :(得分:1)

我希望这对你有所帮助。

<?php
$servername = "host_name";
$username = "username";
$password = "password";
$dbname = "database_name";

$connect = new mysqli($servername, $username, $password, $dbname);

if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
} 

?>
<!DOCTYPE html>
<html>
<head><title>page</title>
<link rel="stylesheet" href="page.css" type="text/css"> </head>
<body>
<div id="wrapper">
<div id="head"></div><br>
<div id="navi"></div><br>
<div id="left-column">
<?php
$query = ""; // Your Query Rigth Here...
$result = $connect->query($query);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) { 
      echo $row["id"];
   }
} else {
    echo "0 results";
}
?>
</div>
<div id="center-column"></div>
<div id="right-column"></div>
   </div> 
</body>
</html>

答案 2 :(得分:1)

<!DOCTYPE html>
<html>
<head><title>page</title>
<link rel="stylesheet" href="page.css"
    type="text/css"> </head>
<body>
<div id="wrapper">
<div id="head"></div><br>
<div id="navi"></div><br>
<div id="left-column">$conn =mysqli_connect('host', 'username', 'password', 'database');

$query = ""; // your sql query

$result = mysqli_query($conn, $query);

 while($row = mysqli_fetch_object($result))
 {
      echo $row->field _name;
       echo $row->field _name2;
 }
    ?>
    </div>
<div id="center-column"></div>
<div id="right-column"></div>
   </div> 
</body>
</html>