MySQLi 的数据库使用(过程式)

MySQL 飞快学 335浏览

数据库的连接

<?php
require_once 'db_config.php';

$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
echo "Connected to sakila successfully";
?>

读取数据

<?php
require_once 'db_config.php';

$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) die("Connection failed: " . mysqli_connect_error());
$sql = "select * from film LIMIT 0 , 10";
$result= mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Sakila 影片</title>
</head>
<body>
<table>
  <tr>
    <td>名称</td>
    <td>发布时间</td>
  </tr>
<?php while($row = mysqli_fetch_assoc($result)) : ?>  
  <tr> 
    <td><?php echo $row["title"];?></td>
    <td><?php echo $row["release_year"];?></td>
  </tr>  
<?php endwhile; ?>   
</table> 
</body>
</html>