Re: problems with functions/included files/mysql resource

From: Date: Wed, 28 Mar 2007 23:50:31 +0000
Subject: Re: problems with functions/included files/mysql resource
References: 1 2 3  Groups: php.db 
Request: Send a blank email to php-db+get-43358@lists.php.net to get a copy of this message
Jvhcom wrote:
Hello Neil and Chris, Here are the database-related functions that reside in their own separate file: function connecttodatabase() {
    global $link_resource;
    if(!($link_resource=mysql_connect('host', 'user_name', 'password'))) {
        printf("Error connecting to host %s, by user %s", 'host', 'user_name');
    exit();
    }
    if(!mysql_select_db('databasename', $link_resource)) {
        printf("Error in selecting %s database", 'databasename');
        printf("ERROR:%d %s", mysql_errno($link_resource), mysql_error($link_resource));
        exit();
    }
} function runquery($dbquery, $link_resource) {
    global $result;
    global $numberRows;
    $result = mysql_query($dbquery, $link_resource);
    if (!$result) {
        printf("Error in executing: %s ", $dbquery);
        printf("ERROR: %d %s", mysql_errno($link_resource), mysql_error($link_resource));
    exit();
    } else {
        $numberRows = mysql_num_rows ($result);
    }
} Here is the dropdown list function that lives in a separate file with other dropdown functions in which I use the database functions. function dd_company($company_id = 0) {
    $dbquery="SELECT id, name from companies where enabled = 'yes' order by name";
    connecttodatabase();
    runquery($dbquery, $link_resource);
The problem is here. Inside this function, 'link_resource' doesn't exist. You can change that easily: function dd_company($company_id=0)
    global $link_resource;
    $dbquery = "SELECT .....";
Problem solved. Also note that unless you are using multiple database connections in the one script, you don't need to pass around the $link_resource. See http://php.net/mysql_query for more info - If the link identifier is not specified, the last link opened by mysql_connect() is assumed. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.db (#43358) next »