我正在尝试为数据库连接创建一个单独的登录文件,因为我不太喜欢在每个需要数据库访问的页面上有所有的访问详细信息。
我在我的服务器上创建了一个单独的文件,其中包含成功登录所需的变量,然后使用;
include_once('path_to_file/filename.php');获取变量,然后使用;
$dbconnection = mysqli_connect("$hostname","$username","$password","$database") or die ("Could not connect to the server");但每次连接都会失败。我尝试在试图包含的文件中包含连接脚本,但随后收到以下消息:
Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)我真的不确定如何解决这个问题,但我的服务器中的每个页面都或多或少地访问数据库,我认为到处复制登录详细信息肯定是一个安全风险!
有人有什么建议或替代方案吗?
数据库日志记录格式为:
<?php
# parameters for connection to MySQL database
$hostname="hostname";
$database="databasename";
$username="username";
$password="password";
?>另外,我也尝试了要求,并得到了同样的结果。
发布于 2014-05-26 18:55:48
此外,当在PHP中使用多个MySQL连接时,您必须提供第四个参数,告诉PHP实际创建如下所示的新连接(如果您使用到同一主机的两个连接,这一点非常重要):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);如果没有使用第四个参数,并且参数相同,那么PHP将返回相同的链接,并且不会建立新的连接。
在此之后,您应该使用带有额外参数的"mysql_query“,而不是定义要使用的连接:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));https://stackoverflow.com/questions/23868296
复制相似问题