kiss-o-matic Posted October 18, 2011 Share Posted October 18, 2011 Okay, some code broke. I was doing this, basically. inc_db.php <?php $link_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error()); mysql_select_db('main_db') or die ('Could not select database'); ?> inc_other_db.php <?php $other_db = mysql_connect('localhost','user','pass') or die('Could not connect: ' . mysql_error()); mysql_select_db('other_db') or die ('Could not select database'); ?> funcs.php <?php function do_something( $a, $b ) { $query = "SELECT id FROM users WHERE a = '$a' AND b = '$b'"; $result = mysql_query( $query ) or die ( 'Query result failed: ' . mysql_error() ); // do something with result } ?> Caveat: One script pulls in inc_other_db.php *after* the inc_db.php so it uses that. New funcs.php <?php function do_something( $a, $b ) { global $linkdb; $query = "SELECT id FROM users WHERE a = '$a' AND b = '$b'"; $result = mysql_query( $query, $linkdb ) or die ( 'Query result failed: ' . mysql_error() ); // do something with result } ?> Runs, but mysql_query() always fails, and mysql_error() always returns a blank error. Any ideas? Something big changed in PHP5 that I'm totally missing? I'm getting jack in the PHP error logs. How can I print out some info about $linkdb in my function? I checked it with if ( !$linkdb) and isset(), and both seemed fine. Totally frustrated. Sleeping now. Quote Link to comment https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/ Share on other sites More sharing options...
kiss-o-matic Posted October 18, 2011 Author Share Posted October 18, 2011 Sorry if I'm incoherent... had a few whiskeys. The script executing this stuff would look like <?php require( inc_db.php ); require( inc_other_db.php ); require( funcs.php ); do_something( "user", "pass" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/#findComment-1280214 Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2011 Share Posted October 18, 2011 Nothing in your code is php version specific. However, using mysql_error(...) after a mysql_query() statement requires the same link resource as a paraemter that the mysql_query() statement used so that it will report any errors that occurred on the same connection that the query was executed on. Quote Link to comment https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/#findComment-1280218 Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2011 Share Posted October 18, 2011 If your database username/password is the same for both databases and you are simply using two different database names, you don't need to make two different connections (the second one is actually just reusing the first connection.) You can either just call mysql_select_db when you need to switch database names or you can use the database name in your query. Instead of table_name in a query, you would use db_name.table_name Quote Link to comment https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/#findComment-1280221 Share on other sites More sharing options...
kiss-o-matic Posted October 19, 2011 Author Share Posted October 19, 2011 Hey Looks like I was doing some stuff before I included the connection to the database I needed. Anyway, all sorted. But, both of your suggestions are wise and I should take them into account. It's a pretty old site, and I was cutting my teeth on PHP. Lots of code to change. :? Quote Link to comment https://forums.phpfreaks.com/topic/249325-db-issues-upgraded-from-4x-to-52/#findComment-1280393 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.