2

Is there anything wrong with connecting and closing to a database by calling the function below with the mysql_query and mysql_fetch_array commands between the two

<?php

function dbconnect()
{   
    $sql = "localhost"; 
    $username = "------";
    $password = "-----";
    $connection = mysql_connect($sql, $username, $password) or 
    die("unwable to cct");
    $databse = mysql_select_db("-------", $connection); 
    global $connection;
}

function close()
{
    global $connection;
    mysql_close($connection);
}

dbconnect();
$query = "Some SQL Statement";
$data = mysql_query($query, $connection); - L1
while (mysql_fetch_assoc($data))
{
  //echo something 
}
close();
?>

At present, I am getting an error saying that $connection at L1 needs to be a resource but is a BOOL. If I give a die statement there, the same is triggered. I have no idea what is wrong. Please spot any errors you can. I have to take a sabbatical from coding and I am back after a while.

Thanks & regards

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
varun Kishnani
  • 169
  • 1
  • 4
  • 16

4 Answers4

2

You must use the global keyword before assigning the $connection variable. Otherwise, you declare a local $connection inside the function and then call a reference to the yet non-existent global $connection. In the other functions, that non-existent global is used.

function dbconnect()
{   
    // Global first to be sure the subsequent $connection is the global
    // rather than a new one local to this function
    global $connection;

    $sql = "localhost"; 
    $username = "------";
    $password = "-----";
    // Now this modifies the global $connection
    $connection = mysql_connect($sql, $username, $password) or die("unwable to cct");
    $databse = mysql_select_db("-------", $connection); 
}

More readable would be to use the $GLOBALS array:

function dbconnect()
{   
    $sql = "localhost"; 
    $username = "------";
    $password = "-----";

    // Using the $GLOBALS superglobal array
    $GLOBALS['connection'] = mysql_connect($sql, $username, $password) or die("unwable to cct");
    $databse = mysql_select_db("-------", $GLOBALS['connection']); 
}

Best of all would be to return $connection from dbconnect() and use that value in other functions:

function dbconnect()
{   
    $sql = "localhost"; 
    $username = "------";
    $password = "-----";
    $connection = mysql_connect($sql, $username, $password) or 
    die("unwable to cct");
    $databse = mysql_select_db("-------", $connection);

    // Return from the function
    return $connection; 
}

// call as 
$connection = dbconnect();
// and define your other functions to accept $connection as a parameter
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Thanks Michael.. declaring $connection as global at the begining of the function was not the problem in the instant case but what is had said here has been very useful.. I now better understand the global keyword – varun Kishnani Nov 16 '12 at 08:16
1

declare global $connection before calling mysql_connect()

function dbconnect()
{   
    global $connection;
    $sql = "localhost"; 
    $username = "------";
    $password = "-----";
    $connection = mysql_connect($sql, $username, $password) or 
    die("unwable to cct");
    $databse = mysql_select_db("-------", $connection); 
}
pn8830
  • 388
  • 3
  • 13
0

Not too sure but try closing it by using

mysql_Close($Connection);

everything else looks good

Jose Rivas
  • 59
  • 1
  • 1
  • 9
0

Just put the global $connection; line in the beginning of the function instead and it should work. Using global keyword at the end of the function implies that you want to use the global variable $connection. But the thing you will be doing now is, assingning global variable $connection "the actual connection resourceID".

crazykru
  • 29
  • 5