I am trying to use fewer resources and processes when making database connections in my scripts, so, upon the first call to make a DB connection, I store the Link Resource ID in a constant variable that can be accessed by all other functions that need a DB connection, like this...
function connect_db() {
if (!defined('_CONNECT_DB')) {
$result = @mysql_pconnect("localhost", "Username", "Password"); @mysql_select_db("database_name"); define('_CONNECT_DB', $result); } return _CONNECT_DB;
I call this function this way...
$query = "SELECT * FROM table"; $connid = connect_db(); $result = mysql_query($query, $connid);
What's odd is that this works for the first query, but after that the Resource Link ID stored in _CONNECT_DB ceases to work, making MySQL issue the error: mysql_query(): 4 is not a valid MySQL-Link resource...
Is there any reason why this isn't working? Normally I store the Resource Link ID in a local variable that I can re-use with no problem, so, I can't understand why storing the Resource Link ID in a constant var won't work? Is it not possible to re-use the same connection for multiple queries to different tables in the DB? I know I can do this if the Resource Link ID is assigned to a local var, so, maybe it can't be stored in a constant var??
Only other solution I can think of is to use non-persistent connections and simply make a connection to the DB then immediately disconnect for each function that needs DB access, but, I'm wondering if this will create more overhead and overload the database with too many connections?
I've searched and read just about everything I could find about exactly how PHP and MySQL work together, but, if there's anything that explains in detail exactly what is happening when you call mysql_connect or mysql_query, please point me in its direction.
Any input on this is most appreciated!
Monty
A define is pretty much for strings only, not objects or resources. Try using $GLOBALS['_CONNECT_DB'] instead.
-- paperCrane <Justin Patrin>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php