0

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

0

3 Answers 3

2

Always use include_once or require_once when including other files.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.