如何在 php 中切换连接文件


How to switch connection file in php?

如何在php中切换连接文件?我有两个数据库,名称是连接.php和连接1.php。当我提交用户名和密码时,我想检查第一个数据库,然后如果结果为空,则需要切换另一个数据库。但是现在它只连接第一个数据库,而不是第二个数据库......我试图先关闭,但它不起作用。请向我建议任何解决方案...

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>";      
    }

这是连接文件:-

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 还 { 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']."/图像/"(; define("UPIMAGEDIR", "http://".$_SERVER['HTTP_HOST']."/zoombox_admin/"(; 定义(USR,'abcdef'(; 定义(DB,'abcd'(;
定义(HST,'111.111.111.111'(; 定义(PWD,'xxx'(; } $con 1=mysql_connect(HST,USR,PWD( 或芯片("连接失败"(; $db=mysql_select_db(DB,$con 1( 或 die("db 未连接"(;

你应该关闭mysql_connect的连接。

只需找到您定义mysql_connect的变量并将其关闭即可。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');;
mysql_close($link);

你能显示连接1.php和连接.php吗?请注意,可以将连接资源(从 mysql_connect(( 获取(作为第二个参数传递给mysql_query,例如:

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

如果查询在第一个连接时失败,它将使用第二个连接。希望对你有帮助