Problem - Losing selected db on mysql calls....
| From: | Scott Brown | Date: | Mon, 02 Oct 2000 01:44:20 +0000 |
| Subject: | Problem - Losing selected db on mysql calls.... | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-3200@lists.php.net to get a copy of this message | ||
If this is a FAQ, kindly point me to it... I cant seem to find any
references around...
I've been banging my head over this most of the night - I'm changing some
ASP code over to PHP (I've seen the light... ;-) and am running into a
problem.
Being a "good little programmer" I've compartmentalized my database
connection into one common routine, which I then "require(...)" into each
ASP page which needs db connectivity... and this worked well under ASP/SQL7,
in that when I needed to change the userid or passowrd, I changed it in one
spot, and I was done.
To this extent, I've tried the same under PHP4.02 / MySQL 3.22.32....
producing:
$connTemp="";
function OpenDSN()
{
$_uid = "....";
$_pwd ="....";
$connTemp = mysql_connect("localhost",$_uid,$_pwd);
if (mysql_select_db("mydb",$connTemp))
echo "<!-- connection to mydb successful --">;
else
echo "<!-- connection to mydb failed with " . mysql_error() . " -->";
}
and place it, along with other standardized routines into a subroutine
file.... $DOCUMENT_ROOT/subs/sub.php
And when called, this produces a valid connection to the database (the
successful message shows in the resultant webpage).
HOWEVER... when I next try to access my database
<? require($DOCUMENT_ROOT . "/subs/sub.php"); ?>
<?
OpenDSN();
$inqSQL = "select .......";
$result = mysql_db_query("mydb",$inqSQL);
if (!$result) {
echo mysql_error();
exit;
}
.....etc...
?>
I get a "bad query : No Database Selected" error being returned....
Is there some sort of scope problem with trying to perform centralized
database management in this manner? Or have I really just putzed some
basic MySQL access??
Thanks
Scott