PHP基础知识(二)

      1 连接MySQL

 

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die('Unable to connect to MySQL: '. mysql_error());

 

 

      2 选择相应的数据库

 

mysql_select_db($db_database) or die('Unable to select database: ' . mysql_error());

 

      3 执行查询

$query = 'SELECT * FROM book';
$result = mysql_query($query);
if(!$result) die('Database access failed: ' . mysql_error());
$rows = mysql_num_rows($result);
for($j = 0; $j < $rows; ++$j){
    $row = mysql_fetch_row($result);
    echo 'Author: ' . $row[0] . '<br>';
    echo 'Title: ' . $row[1] . '<br>';
}

 

      4 关闭连接

mysql_close($db_server);

 

 

你可能感兴趣的:(php基础)