PHP基础知识(三)——使用mysqli

   使用mysqli

      1 链接MySQL

 

$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if($connection->connect_error) die($connection->connect_error)

 

      2 执行查询

$query = "SELECT * FROM classics";
$result = $connection->query($query);
if(!$result) die($connection->error);

 

      3 解析结果集

$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if($connection->connect_error) die($connection->connect_error)
$query = "SELECT * FROM classics";
$result = $connection->query($query);
if(!result) die($connection->$error);
$rows = $result->num_rows;
for($j = 0; $j < $rows; ++$j){
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_ASSOC);
    echo 'Author: ' . $row['author'] . '<br>';
    echo 'Title: ' . $row['title'] . '<br>';
}
$result->close();
$connection->close();

 

你可能感兴趣的:(mysql)