problems with functions/included files/mysql resource
problems with functions/included files/mysql resource
am 28.03.2007 05:50:40 von Jvhcom
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):
//the following defines functions to connect to the data base and submit
queries
require('databasefunctions.php');
/ /the following defines a function that uses above database functions
to build a dropdown list from database tables
require('dropdownlistfunctions.php');
//some if/then statements
//...
//...
//the following builds a form that is displayed. It uses a function from
dropdownlistfunctions.php (above) to build a dropdown list.
require('build_form.php');
?>
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
Re: problems with functions/included files/mysql resource
am 28.03.2007 06:15:26 von Niel Archer
Hi,
>
> In the function to connect to and select the database, I make the mysql-link
> resource a global value.
It sounds like you've misunderstood 'global' variable. The global
statement needs to go inside the function where you want to use the
variable, not where you assign it. This tells that function to access
that variable from the global scope. You will need to have this
variable available at the global scope. Creating it inside another
function will not do, you must return that from the function to the
global scope too
e.g.
$db = new mysql;
... some code ...
function UseDb()
{
global $db;
... some more code ...
}
?>
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: problems with functions/included files/mysql resource
am 28.03.2007 06:23:21 von dmagick
> 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?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: problems with functions/included files/mysql resource
am 28.03.2007 21:04:23 von Jvhcom
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();
}
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 .= "
if($company_id == $row[id]) {
$dd_company .= " selected>$row[name]\n";
} else {
$dd_company .= ">$row[name]\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" wrote in message
news:4609EDB9.3090900@gmail.com...
>
>> 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
Re: problems with functions/included files/mysql resource
it would be helpful if you pasted to us the error u are getting. So instead
of runquery($dbquery, $link_resource);
use this:
runquery($dbquery, $link_resource) or die(mysql_error());
which will echo out the error.
On 3/28/07, 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);
> global $dd_company;
> $dd_company .= "\n";
> while ($row=mysql_fetch_array($result)) {
> $dd_company .= "
> if($company_id == $row[id]) {
> $dd_company .= " selected>$row[name]\n";
> } else {
> $dd_company .= ">$row[name]\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" wrote in message
> news:4609EDB9.3090900@gmail.com...
> >
> >> 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
>
>
------=_Part_23218_31800831.1175120031119--
Re: problems with functions/included files/mysql resource
am 29.03.2007 01:50:31 von dmagick
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.