利用PHP訪問到mysql數(shù)據(jù)庫的方法
訪問過程如下:
//連接數(shù)據(jù)庫方式1
$conn = new mysqli('localhost', 'root', 'password', 'test');
//連接數(shù)據(jù)庫方式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'];
}
//查詢代碼
//釋放結(jié)果集+關(guān)閉mysql連接
$query1 -> free_result();
$conn -> close();