我是 PHP 和 MySQL 的新手,我有一个关于 mysql_connect 和 mysql_close 的问题
这是我的代码(functions.php):
$link = mysql_connect("localhost","root","") or die("error");
mysql_select_db("dbName",$link) or die("error 2");
mysql_query("SET NAMES UTF8");
function get_title($param)
{
//top of the function
$sql = sprintf("SELECT title FROM pages WHERE id='%s'",
mysql_real_escape_string($param));
$result = mysql_query($sql);
$title = mysql_result($result, 0,0);
echo trim($title);
//inside the function
//bottom of the function
}
//under the function
我从 page.php 调用这个函数。但我不确定在哪里关闭此连接。我应该在函数内部关闭它吗?我应该在功能下关闭它吗?我应该在函数顶部连接并关闭函数底部吗?
顺便说一句,可以随意改进我的代码。