Jump to content

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.