For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > March 2007 > Re: [PHP-DB] problems with functions/included files/mysql resource









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] problems with functions/included files/mysql resource
Chris

2007-03-28, 3:58 am


> 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/
Jvhcom

2007-03-28, 6:58 pm

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@gmail.com> wrote in message
news:4609EDB9.3090900@gmail.com...
>
>
> 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/

Onochie Anyanetu

2007-03-28, 6:58 pm

Hi

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 <jvhcom@yahoo.com> 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 .= "<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@gmail.com> wrote in message
> news:4609EDB9.3090900@gmail.com...
> etc..
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Chris

2007-03-28, 9:59 pm

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/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com