views:

294

answers:

3

I have this little function do connect to a MySQL database:

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).

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

What is wrong with my code? Thanks

+1  A: 

First, search all of your code for 'function connectSugarCRM()' and make sure it appears once and only once. 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.

Scott Saunders
Scott: 1-I'm totally sure that the function is only defined once and there is no recursive includes. 2-I need to identify the database connector, because I'm using more than one database in the same procedure. 3 - Line Error - fixed
Armadillo
I've changed my code suggestion to handle your #2. It really looks like that function is defined or included more than once. Maybe you can post more of your code so we can check for multiple includes. I've been "totally sure" and wrong so many times...
Scott Saunders
+1  A: 

Check your code for recursive includes.

The module that contains connectSugarCRM() seems to be included twice:

<?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
Quassnoi
+2  A: 

Always use include_once or require_once when including other files.

Matt