Jump to content

Warning: mysql_query() in php function after move to php / mysql 5


Recommended Posts

hi, i've got a few php pages i've made which connect to mysql at the top of each page like so:

 

$kevlabels = mysql_connect($hostname_kevlabels, $username_kevlabels, $password_kevlabels) or trigger_error(mysql_error(),E_USER_ERROR);

 

i then run a few select statements off this which works fine such as:

 

mysql_select_db($database_kevlabels, $kevlabels);
$query_release = sprintf("SELECT releaseid, catnumber FROM release2 WHERE catnumber = %s", GetSQLValueString($colname_release, "text"));
$release = mysql_query($query_release, $kevlabels) or die(mysql_error());
$row_release = mysql_fetch_assoc($release);
$totalRows_release = mysql_num_rows($release);

 

 

now here's the problem since the move...

 

... then in the body of my page, i call a function like this:

 

 <?php $task = '65'; echo tep_get_userlog($task, $releaseid); ?>

 

here's the simple function:

 

function tep_get_userlog($logid, $releaseid) {
// gets the username of a person
$query = sprintf("SELECT username FROM users u, tasklogs logs WHERE u.userid = logs.userid AND logs.releaseid = '" . $releaseid . "' AND logs.taskid = '" . $logid . "'");

mysql_select_db($database_kevlabels, $kevlabels);
$results = mysql_query($query, $kevlabels) or die(mysql_error());	

$row_results = mysql_fetch_assoc($results);
$result=$row_results['username'];
return $result;
}

 

and where the result of this should be displayed i get this error:

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource

 

the line that gives the error is this:

 

mysql_select_db($database_kevlabels, $kevlabels);

 

i have no idea why this has stopped working, i use the same connection string in the page and it works!! but within the function it doesn't :( and searched the interwebby for hours!!

if anyone's got any ideas that would be much appreciated!!!

i've tried closing the connection and opening again to no avail.

 

thanks in advance :)

kev.

 

 

 

Where is $hostname_kevlabels, $username_kevlabels etc. coming from?  Are you setting these values in the script or getting them from a form/querystring of some kind?

 

Quite often problems with server moves are related to a difference in the 'Register Globals' settings between servers.

 

Regards

Huggie

function tep_get_userlog($logid, $releaseid) {

// gets the username of a person

        global $kevlabelsl;

        global $database_kevlabels;

$query = sprintf("SELECT username FROM users u, tasklogs logs WHERE u.userid = logs.userid AND logs.releaseid = '" . $releaseid . "' AND logs.taskid = '" . $logid . "'");

 

mysql_select_db($database_kevlabels, $kevlabels);

$results = mysql_query($query, $kevlabels) or die(mysql_error());

 

$row_results = mysql_fetch_assoc($results);

$result=$row_results['username'];

return $result;

}

 

Should work now.

Your mysql_select_db($database_kevlabels, $kevlabels); statement in the tep_get_userlog function was not working before but the display_errors and/or error_reporting settings prevented the output of the Warning message.

 

The mysql_query in the tep_get_userlog function call works because you have already selected a database in your main code. The mysql_select_db() function call in the tep_get_userlog function is unnecessary (unless you want to select a different database) so when it fails there is no effect on the rest of the code.

 

The mysql_query in the tep_get_userlog function is probably seeing that $kevlabels is un-defined and is using the existing link.

 

Unless you are selecting a different database, there is no point in having more than one mysql_select_db() statement. Any additional ones beyond the first one in the main code are just wasting processor time.

Where is $hostname_kevlabels, $username_kevlabels etc. coming from?  Are you setting these values in the script or getting them from a form/querystring of some kind?

 

Quite often problems with server moves are related to a difference in the 'Register Globals' settings between servers.

 

Regards

Huggie

 

these values are set in a file that's included at the very beginning..  like

 

$hostname_kevlabels = "blahblach"; 

 

i'll try turning on register globals for now and see if it fixes it :D

or can i force the variables to work globally somehow?

i've tried putting the following in my apache directives and restarting apache:

 

php_flag register_globals on

 

which still throws up the same error.

 

i've also tried moving all my code for the functions to the same file which is included at the very top of each page.. (to immediately after my connection string and each connection variable)

 

i've also removed

 

mysql_select_db($database_kevlabels, $kevlabels);

 

from each of my functions :)

 

ai'm still getting the same message so far....

global $kevlabels;

global $database_kevlabels;

 

i've just added these bits to my function and it works like a treat!!!!

 

thankyou very much!!!!

 

i'll try without registerglobals in my directives and see if it's still all cool :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.