php5.0 连接数据 用之前的方法
$con=mysqli_connect("localhost","my_user","my_password","my_db");
总是出现Warning!请使用 PDO 和 mysqli .
那我就改写用 mysqli
$conn = new mysqli(“localhost”, “root”, “123”, “db”);
if ($conn->connect_error) {
trigger_error(‘Database connection failed: ‘ . $conn->connect_error, E_USER_ERROR);
}
$conn->query("SET NAMES ‘UTF8‘"); //设置编码
连接完数据库,简单使用查询测试:
$result = $conn->query("SELECT * FROM news");
查询出来得结果分为Array 和 Object
如果是Array:
while ($row = $result->fetch_array()) { echo ($row["name"]) . "<br>"; echo ($row["author"]) . "<br>"; echo ($row["time"]) . "<br>"; echo "<hr>"; }
如果是Object:
while($row=$result->fetch_object()) { echo ‘‘.$row->content; //content-->是表的字段名 }
php mysqli 连接数据库,布布扣,bubuko.com
时间: 2024-10-11 08:50:02