【问题标题】:Connecting to MySQL database with PHP使用 PHP 连接 MySQL 数据库
【发布时间】:2010-10-18 01:37:00
【问题描述】:

我有这个小功能可以连接到 MySQL 数据库:

function connectSugarCRM()
{
    $connectorSugarCRM = mysql_connect ("localhost", "123", "123")
    or die ("Connection failed");
    mysql_select_db("sugar5") or die ("Failed attempt to connect to database");
    return $connectorSugarCRM;
}

然后,为了运行一个查询,我正在做这样的事情,但我总是得到一个“PHP 致命错误:无法重新声明 connectSugarCRM()(之前在...中声明”,它指向我的定义函数“connectSugarCRM”(第 1 行)。

$ExecuteSQL = mysql_query ($sqlSTR, connectSugarCRM()) or die ("Query Failed!");

我的代码有什么问题? 谢谢

【问题讨论】:

    标签: php mysql connection


    【解决方案1】:

    首先,在您的所有代码中搜索“function connectSugarCRM()”并确保它只出现一次。如果不止一次出现,那就是你的问题了。

    否则,请尝试将您的查询行更改为:

    $sugarConnection = connectSugarCRM();
    $ExecuteSQL = mysql_query($sqlSTR, $sugarConnection) or die ("Query Failed!");
    

    而且在未来,行号和完整的错误信息对于调试这些东西真的很有帮助。

    【讨论】:

    • Scott:1-我完全确定该函数只定义了一次,并且没有递归包含。 2-我需要识别数据库连接器,因为我在同一过程中使用了多个数据库。 3 - 线路错误 - 已修复
    • 我已更改我的代码建议以处理您的 #2。看起来该函数被多次定义或包含。也许您可以发布更多代码,以便我们检查多个包含。我已经“完全确定”并且错了很多次......
    【解决方案2】:

    检查您的代码是否存在递归包含。

    包含connectSugarCRM()的模块好像被包含了两次:

    <?php
    function connectSugarCRM()
    {
        $connectorSugarCRM = mysql_connect ("myserver", "myname", "mypass") or die ("Connection failed\n");
        mysql_select_db("test") or die ("Failed attempt to connect to database\n");
        return $connectorSugarCRM;
    }
    
    function connectSugarCRM()
    {
        $connectorSugarCRM = mysql_connect ("myserver", "myname", "mypass") or die ("Connection failed\n");
        mysql_select_db("test") or die ("Failed attempt to connect to database\n");
        return $connectorSugarCRM;
    }
    
    $ExecuteSQL = mysql_query ("SELECT 1", connectSugarCRM()) or die ("Query Failed!\n");
    ?>
    
    [~]# php test.php
    
    PHP Fatal error:  Cannot redeclare connectsugarcrm() (previously declared in /root/test/sugar/test.php:4) in /root/test/sugar/test.php on line 14
    

    【讨论】:

      【解决方案3】:

      在包含其他文件时始终使用 include_once 或 require_once。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多