我的日历脚本工作得很好,直到我将其包含到我的模板中。 events.php 中的 html 格式正确,但未处理与数据库的连接,导致出现下面的错误。为什么会发生这种情况,连接丢失/未处理?
<?php include ROOT . '/files/cal/events.php';?>
错误:
警告:mysql_connect() [function.mysql-connect]:在线/home/social/public_html/files/cal/smoothcalendar.php 中的用户“nobody”@“localhost”(使用密码:NO)访问被拒绝19
警告:mysql_select_db() 期望参数 2 为资源, bool 值在/home/social/public_html/files/cal/smoothcalendar.php 第 21 行给出
警告:mysql_query() 期望参数 2 为资源, bool 值在/home/social/public_html/files/cal/smoothcalendar.php 第 195 行给出
无法运行查询: 警告:mysql_close() 期望参数 1 为资源, bool 值在/home/social/public_html/files/cal/smoothcalendar.php 第 38 行给出
smoothcalendar.php
<?php
$server = "localhost";
$username = "****";
$password = "****";
$dbname = "***_socialdb";
class SmoothCalendar {
private $options = null,
$get,
$post;
private $connection;
function __construct($options)
{
$this->connection = mysql_connect($GLOBALS["server" ],
$GLOBALS["username"],
$GLOBALS["password"]);
mysql_select_db($GLOBALS["dbname"],$this->connection);
最佳答案
您可能将 include
放在函数或其他内容中,即不在全局上下文中。这就是为什么无法使用 $GLOBALS 数组访问您的变量...它们不是全局的。
这应该有效:
<?php
class SmoothCalendar {
private $options = null,
$get,
$post;
private $connection;
private $server = "localhost";
private $username = "****";
private $password = "****";
private $dbname = "***_socialdb";
function __construct($options)
{
$this->connection = mysql_connect($this->server,
$this->username,
$this->password);
mysql_select_db($this->dbname, $this->connection);
关于php - Mysql:在 Include() 后连接到数据库丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9156464/