简体   繁体   English

使用PHP连接到MySQL数据库

[英]Connecting to MySQL database with PHP

I have this little function do connect to a MySQL database: 我有这个小功能可以连接到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;
}

And then, to run a query, I'm doing something like this, but I allways get an "PHP Fatal error: Cannot redeclare connectSugarCRM() (previously declared in ...", which points to the definition of my function "connectSugarCRM" (line 1). 然后,要运行查询,我正在执行类似的操作,但是我总是收到“ PHP致命错误:无法重新声明connectSugarCRM()(先前在...中声明”),它指向我的函数“ connectSugarCRM的定义”(第1行)。

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

What is wrong with my code? 我的代码有什么问题? Thanks 谢谢

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

First, search all of your code for 'function connectSugarCRM()' and make sure it appears once and only once. 首先,在所有代码中搜索“ function connectSugarCRM()”,并确保其仅出现一次。 If it's there more than once, that's your problem. 如果不止一次,那就是您的问题。

Otherwise, try changing your query line to this: 否则,请尝试将查询行更改为此:

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

And in the future, the line numbers and full error messages are really helpful for debugging this stuff. 将来,行号和完整的错误消息对于调试这些东西确实很有帮助。

Check your code for recursive includes. 检查您的代码是否包含递归。

The module that contains connectSugarCRM() seems to be included twice: 包含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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM