在过去,我使用一个简单的方法没有任何问题:
$link = db_connect();
在具有多个 SQL 命令的单个文件中,如下所示:
$sql = "UPDATE table SET...";
$sql_result = mysql_query($sql, $link)
or die("Couldn't execute query.");
$sql2 = "UPDATE table2 SET...";
$sql_result2 = mysql_query($sql2, $link)
or die("Couldn't execute query.");
现在,如果没有多个/单独的 db_connect,我创建的任何新文件都将无法工作。例如:
$link = db_connect();
$link2 = db_connect();
$sql = "UPDATE table SET...";
$sql_result = mysql_query($sql, $link)
or die("Couldn't execute query.");
$sql2 = "UPDATE table2 SET...";
$sql_result2 = mysql_query($sql2, $link2)
or die("Couldn't execute query.");
具有多个 mysql_queries 引用单个 db_connect() 的旧文件;仍然工作正常。有什么可以改变的?
谢谢。
更新为包含 db_connect(); 功能:
function db_connect($db="database", $host="localhost", $user="user", $p="password") {
$dbcnx = @mysql_connect($host, $user, $p);
if (!$dbcnx)
{
echo( "<p>Unable to connect to the database server at this time.</p>" );
exit();
}
$database = @mysql_select_db($db, $dbcnx);
if (!$db)
{
echo "<p>Unable to locate the database at this time.</p>";
exit();
}
return $dbcnx;
}