Re: problems with functions/included files/mysql resource

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

 



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);
    global $dd_company;
    $dd_company .= "<option></option>\n";
    while ($row=mysql_fetch_array($result)) {
        $dd_company .= "<Option value=\"$row[id]\"";
        if($company_id == $row[id]) {
            $dd_company .= " selected>$row[name]</option>\n";
        } else {
            $dd_company .= ">$row[name]</option>\n";
        }
    }
}

Lastly, I call the dd_company() function with the intent to use the 
resulting $dd_company dropdown.

The error is generated in that last function on the 'runquery($dbquery, 
$link_resource);' line.

So, are you saying that I should be making the $link_resource global in the 
runquery function instead of the connecttodatabase function?

I would like to use the database functions if I can get it right. In the 
past I've always just included separate files that contained the statements 
rather than defining functions. That works fine, but I'd rather be able to 
do it this way.

"Chris" <dmagick@xxxxxxxxx> wrote in message 
news:4609EDB9.3090900@xxxxxxxxxxxx
>
>> In the function to connect to and select the database, I make the 
>> mysql-link resource a global value.
>
> How are you doing that? Code please :) Obviously change the password etc..
>
>
> And how are you referencing it in the other files/functions?
>
> -- 
> 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