kev@num Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/ Share on other sites More sharing options...
HuggieBear Posted April 22, 2008 Share Posted April 22, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523860 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523863 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2008 Share Posted April 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523878 Share on other sites More sharing options...
kev@num Posted April 22, 2008 Author Share Posted April 22, 2008 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 or can i force the variables to work globally somehow? Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523897 Share on other sites More sharing options...
kev@num Posted April 22, 2008 Author Share Posted April 22, 2008 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.... Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523905 Share on other sites More sharing options...
kev@num Posted April 22, 2008 Author Share Posted April 22, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523909 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2008 Share Posted April 22, 2008 Don't turn on register globals. They don't have anything to do with functions and variable scope (which is what is the cause of the problem.) Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523912 Share on other sites More sharing options...
kev@num Posted April 22, 2008 Author Share Posted April 22, 2008 it's turned off again now and works like a treat Quote Link to comment https://forums.phpfreaks.com/topic/102314-warning-mysql_query-in-php-function-after-move-to-php-mysql-5/#findComment-523913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.