<?php
header("content-type:text/html;charset=utf-8");
$conn = mysql_connect("localhost", "root", "123456");//连接数据库
mysql_select_db("style");//连接这个名字的数据库
mysql_query("set names utf8");//设置字符集
$sql = "select * from test";//查询这个表
$result = mysql_query($sql);//结果
while ($row=mysql_fetch_assoc($result)) {
echo "<h2>姓名:{$row['name']} 年龄:{$row['age']} 学校:{$row['sch']}</h2>";
}
?>
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "style";
// 创建连接 面向过程写法
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
mysqli_query($conn,'set names utf8');//设置字符集
$sql = "SELECT id, name, age,sch FROM test";//查询这个表
$result = mysqli_query($conn, $sql);//结果
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "<h2>姓名:{$row['name']} 年龄:{$row['age']} 学校:{$row['sch']}</h2>";
}
} else {
echo "0 结果";
}
mysqli_close($conn);
?>
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "style";
// 创建连接 面向对象写法
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
mysqli_query($conn,'set names utf8');
$sql = "SELECT id, name, age,sch FROM test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "<h2>姓名:{$row['name']} 年龄:{$row['age']} 学校:{$row['sch']}</h2>";
}
} else {
echo "0 结果";
}
$conn->close();
?>