原文链接http://developer.51cto.com/art/200911/164808.htm
<?php
/*ConnecttoaMySQLserver连接数据库服务器*/
$
link
=
mysqli_connect
(
'localhost',/*Thehosttoconnectto连接MySQL地址*/
'user',/*Theusertoconnectas连接MySQL用户名*/
'password',/*Thepasswordtouse连接MySQL密码*/
'world');/*Thedefaultdatabasetoquery连接数据库名称*/
if(!$link){
printf("Can'tconnecttoMySQLServer.Errorcode:%s",mysqli_connect_error());
exit;
}
/*Sendaquerytotheserver向服务器发送查询请求*/
if($
result
=
mysqli_query
($link,'SELECTName,PopulationFROMCityORDERBYPopulationDESCLIMIT5')){
print("Verylargecitiesare:");
/*Fetchtheresultsofthequery返回查询的结果*/
while($
row
=
mysqli_fetch_assoc
($result)){
printf("%s(%s)",$row['Name'],$row['Population']);
}
/*Destroytheresultsetandfreethememoryusedforit结束查询释放内存*/
mysqli_free_result($result);
}
/*Closetheconnection关闭连接*/
mysqli_close($link);
?>
mysqli_connect() 连接数据库
最新推荐文章于 2024-03-06 14:22:21 发布