PHP mysql_close()关闭MySQL数据库连接

时间:5年前   阅读:4543

mysql_close()函数用于关闭 MySQL 连接

bool mysql_close ([ resource $link_identifier = NULL ] )

  mysql_close() 关闭指定的连接标识所关联的到 MySQL 服务器的非持久连接。如果没有指定 link_identifier,则关闭上一个打开的连接

  所以,一个比较完整的php操作数据库扩展函数的程序如下所示

<?php
//连接数据库
$link = mysql_connect('localhost','root','******');
if(!$link){
    die('连接失败:'.mysql_error());
}
//选择数据库
mysql_select_db('bookstore',$link) or die('不能选定数据库bookstore:' .mysql_error());

//执行SQL命令
$insert = "insert into books(bookname, publisher, author, price, detail) values
('PHP','电子工业出版社','张三','80.00','PHP相关'),
('ASP','电子工业出版社','李四','90.00','ASP相关'),
('JSP','电子工业出版社','王五','70.00','JSP相关')";
$result = mysql_query($insert);

//操作结果集
$result = mysql_query("SELECT id,bookname,publisher,author,price FROM books");
echo '<table border="1" width="800" class="table">';
echo '<tr>';
echo '<th>编号</th>';
echo '<th>书名</th>';
echo '<th>出版社</th>';
echo '<th>作者</th>';
echo '<th>价格</th>';
echo '</tr>';
while($assoc = mysql_fetch_assoc($result)) {
    echo '<tr>';
    echo "<td>{$assoc['id']}</td>";
    echo "<td>{$assoc['bookname']}</td>";
    echo "<td>{$assoc['publisher']}</td>";
    echo "<td>{$assoc['author']}</td>";
    echo "<td>{$assoc['price']}</td>";
    echo '</tr>';
}
echo '</table>';

//释放结果集
mysql_free_result($result);
//关闭数据库连接
mysql_close($link);
?>

本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。

微信扫码关注

更新实时通知

上一篇:JS阻塞嵌入JS导致CSS阻塞加载的问题

下一篇:java.lang.object

网友评论

请先 登录 再评论,若不是会员请先 注册