利用PHP访问到mysql数据库的方法
访问过程如下:
//连接数据库方式1
$conn = new mysqli('localhost', 'root', 'password', 'test');
//连接数据库方式2
// $conn = new mysqli();
// $conn -> connect('localhost', 'root', 'password', 'test');
//check connection (检查PHP是否连接上mysql)
if ($conn -> connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
//查询代码
$sql = “select * from db_table”;
$query = $conn->query($sql);
while($row = $query->fetch_array()){
echo $row['title'];
}
//查询代码
//释放结果集+关闭mysql连接
$query1 -> free_result();
$conn -> close();