-1

How to switch connection file in php? I have two database whose name is connection.php and connection1.php. when i submit username and password i want to check first database and then if it results empty then need to switch another database. but right now it is connecting only first database and not to second one... I tried to close first but its not working. Plese suggest me any solution...

enter code here
 if(!empty($_POST['uname']) && !empty($_POST['pwd']))
{   
    $flag=0;
    if($flag==0)
    {

    require_once('connection1.php');    

    $q="select * from user_login where (u_name='".$_POST['uname']."' or eid='".$_POST['uname']."') and password='".$_POST['pwd']."' and status=1";
    //echo $q;
    $res=mysql_query($q);

    $row=mysql_fetch_assoc($res);
    }   
        if(empty($row))
        {
            $flag=1;
            mysql_close($res);
        }


    if($flag==1)
    {

    require_once('connection.php'); 

    $q1="select * from user_login where (u_name='".$_POST['uname']."' or eid='".$_POST['uname']."') and password='".$_POST['pwd']."' and status=1";

    $res1=mysql_query($q1);
    //echo "Database : ".mysql_db_name($res1);
    $row1=mysql_fetch_assoc($res1);

    }
}

if(!empty($row))
{

        $u_id=$row['u_id'];
        //header("location:../index.php");      

        echo "<script>parent.location='Test.php'</script>";
    }

    else if(!empty($row1))
    {
        $u_id=$row1['u_id'];
        //header("location:../index.php");      
    //  print_r($row1);
        echo "<script>parent.location='Test1.php'</script>";
    }

    else 

    {

        $err = "<font color='red'>Incorrect Login Information</font>";      

    }

This is connection File:-

enter code here

error_reporting(0);
 $app="web";
 if($app=="local")
 {
define("SITEROOT",      "http://".$_SERVER['HTTP_HOST']."/");
define("ABSPATH",       "c://xampp/htdocs/ujjwal/");
define("SITEJS",        "http://".$_SERVER['HTTP_HOST']."/js2/");
define("SITECSS",       "http://".$_SERVER['HTTP_HOST']."/css/");
define("IMAGEDIR",      "http://".$_SERVER['HTTP_HOST']."/images/");
define("UPIMAGEDIR",        "http://".$_SERVER['HTTP_HOST']."/abcd/abcd/");
define(USR,'root');
define(DB,'xxxx');
define(HST,'localhost');
define(PWD,'');

} enter code here else { define("SITEROOT", "http://".$_SERVER['HTTP_HOST']."/"); define("SITEJS", "http://".$_SERVER['HTTP_HOST']."/js2/"); define("SITECSS", "http://".$_SERVER['HTTP_HOST']."/css/"); define("IMAGEDIR", "http://".$_SERVER['HTTP_HOST']."/images/"); define("UPIMAGEDIR", "http://".$_SERVER['HTTP_HOST']."/zoombox_admin/"); define(USR,'abcdef'); define(DB,'abcd');
define(HST,'111.111.111.111'); define(PWD,'xxx'); } $con1=mysql_connect(HST,USR,PWD) or die ("connection failed"); $db=mysql_select_db(DB,$con1) or die("db not connected");

pranali
  • 63
  • 12
  • Can you include those connection files (with passwords censored, ofc)? – Rogue Sep 28 '13 at 12:38
  • **NOTE:** You are vulnerable to SQL injection attacks. Besides that you are using the deprecated `mysql_*` functions. It is recommended to change to `mysqli_*` or PDO. Read [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) on how to protect yourself against SQL injection. – Sumurai8 Sep 28 '13 at 12:42

2 Answers2

0

You are supposed to close the connection of mysql_connect.

Just find variable where you have defined your mysql_connect and close it.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');;
mysql_close($link);
aaron
  • 697
  • 4
  • 11
  • Have you checked `error logs`. what are the errors that you are getting? – aaron Sep 28 '13 at 12:56
  • there is no error log.. just it is not connecting other database because first database is doesn't close it remain open. i used mysqli_close() function but its not working.. – pranali Sep 30 '13 at 07:15
  • @pranali: Are you using `mysqli_close()` or `mysql_close()`? I assume you are using `mysql_close()` because that is what is mentioned in your code.Secondly, they are two separate libraries so if you are connecting using `mysql_connect()` you have to use `mysql_close()` And you need to close the connection string for $con1 as follow in the code: `$row=mysql_fetch_assoc($res); } if(empty($row)) { $flag=1; mysql_close($con1); } ` – aaron Sep 30 '13 at 08:09
  • I am using mysql_close() – pranali Sep 30 '13 at 10:08
0

Can you show connection1.php and connection.php? Notice that you can pass to mysql_query a connection resource (obtain it from mysql_connect()) as second parameter, for example:

$connection1 = mysql_connect('blah', 'blah', 'blah');
$connection2 = mysql_connect('blah2', 'blah2', 'blah2');
if (!mysql_query($query, $connection1)) 
    mysql_query($query, $connection2);

if query fails at first connection, it uses the second one. Hope it helps

kao3991
  • 402
  • 2
  • 8