Functions, SQL Connects and Global Variable

2019-02-27 15:20发布

问题:

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

回答1:

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


回答2:

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); 
}


回答3:

Not too sure but try closing it by using

mysql_Close($Connection);

everything else looks good



回答4:

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