[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-db
Subject:    Re: [PHP-DB] problems with functions/included files/mysql resource
From:       Chris <dmagick () gmail ! com>
Date:       2007-03-28 23:50:31
Message-ID: 460AFF47.3000506 () gmail ! com
[Download RAW message or body]

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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic