Fedexpress Posted October 18, 2008 Share Posted October 18, 2008 Here is my file named "common_db.inc" which I include in every code of database connectivity so that password is not visible , <?php $dbhost='localhost'; $dbpassword='beat'; $dbusername='root'; $default_dbname='mysql'; $MYSQL_ERROR=''; $MYSQL_ERRORNO=''; function db_connect() { global $dbhost,$dbusername,$dbpassword,$default_dbname; global $MYSQL_ERROR ,$MYSQL_ERRORNO; $link_id=mysql_connect($dbhost,$dbusername,$dbpassword); if(! $link_id) { $MYSQL_ERRORNO=0; $MYSQL_ERROR="Connection failed to the $dbhost"; return 0; } else if(empty($dbname)&& !mysql_select_db($default_dbname)) { $MYSQL_ERRORNO=mysql_errno(); $MYSQL_ERROR=mysql_error(); return 0; } else return $link_id; } function sql_error() { global $MYSQL_ERROR ,$MYSQL_ERRORNO; if(empty($MYSQL_ERROR)) { $MYSQL_ERROR=mysql_error(); $MYSQL_ERRORNO=mysql_errno(); } return "$MYSQL_ERRORNO : $MYSQL_ERROR"; } ?> Now, the problem is that I am connecting to aniother database by the use of this code , but am unable to do so using this code <?php include "./common_db.inc"; $link=db_connect('sample_db'); $result=mysql_query("SELECT * FROM user",$link); while($query_data=mysql_fetch_row($result)) { echo ",",$query_data[1]," is a ",$query_data[3],"<br>"; } ?> Here It would not connect to the "sample_db" database until I make a change in "common_db.inc" and set the "$default_dbname=sample_db" , If I let it be "mysql " as it was usually then it wouldn;t connect , Any Hints at where I am wrong!!! Link to comment https://forums.phpfreaks.com/topic/128967-solved-default-database-problem/ Share on other sites More sharing options...
wildteen88 Posted October 18, 2008 Share Posted October 18, 2008 Change include "./common_db.inc"; $link=db_connect('sample_db'); to include "./common_db.inc"; $default_dbname='sample_db'; $link=db_connect(); Link to comment https://forums.phpfreaks.com/topic/128967-solved-default-database-problem/#findComment-668615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.