利用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();