首页 期权学习期权知识正文

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

xiaojiucai 期权知识 2020-08-18 478 0

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/post/7994.html

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

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。