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

2007-03-28 Thread 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();
}

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 [EMAIL PROTECTED] 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



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

2007-03-28 Thread Chris

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



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

2007-03-27 Thread 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.

?php

  $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: [PHP-DB] problems with functions/included files/mysql resource

2007-03-27 Thread Chris


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