Re: problems with functions/included files/mysql resource

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux