RSilva
RSilva

Reputation: 6943

Connecting to MySQL database with PHP

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

Upvotes: 0

Views: 1211

Answers (3)

Scott Saunders
Scott Saunders

Reputation: 30414

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.

Upvotes: 1

Matt
Matt

Reputation: 2230

Always use include_once or require_once when including other files.

Upvotes: 2

Quassnoi
Quassnoi

Reputation: 425823

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

Upvotes: 1

Related Questions