Re: [PHP-DB] problems with functions/included files/mysql resource
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 .= "\n"; while ($row=mysql_fetch_array($result)) { $dd_company .= " wrote in message news:[EMAIL PROTECTED] > >> 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
[PHP-DB] problems with functions/included files/mysql resource
Hello Everyone, I am new to this or any news group. The end of this story is that I believe I am calling a function from within a function in a way that I should not be, but I cannot figure out exactly what part I am doing incorrectly. I have the following main file with other content included from outside files (summarized): When I use the functions from databasefunctions.php directly in the main_file.php to connect to a database and run a query, the functions work successfully. However, when I use those functions from within a function defined inside the dropdownlistfunctions.php, I get an error "Undefined variable: [variable name]". The [variable name] is supposed to contain the mysql-link resource. Since it isn't there, I also get additional warnings that the mysql_query didn't receive a valid mysql-link resource. In the function to connect to and select the database, I make the mysql-link resource a global value. Inside the dropdownlistfunctions.php, when I simply write the statements directly in the function to connect to and select the database, and run the query, that works too. Is there a basic rule about including/requiring files, and using functions within functions that I am not abiding by? It feels as though I am just trying to do some particular thing that you can't do when working with functions that use other functions ... and maybe to do with including/requiring files as well. Any wisdom would be eagerly and gratefully consumed. Kindest regards, JH -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php