简体   繁体   English

使用php从多个数据库和多个表中检查数据

[英]Checking Data from multiple database and multiple table using php

I am trying to make a code that checks data from multiple databases having the similar table. 我正在尝试编写一个代码,以检查来自具有相似表的多个数据库中的数据。 so I made an array of databases and table in which I want to check the data is present or not as given below 所以我做了一个数据库和表的数组,我要在其中检查数据是否存在,如下所示

$DatabaseDetails=array(
    array('DatabaseName'=>'database1','TableName'=>'table1'),
    array('DatabaseName'=>'database2','TableName'=>'table2'),
    array('DatabaseName'=>'database3','TableName'=>'table3')
);

Then I tried some code that if it does not find the data in 1st database it move to second one and so on until it find the data else "print no data in database" but i didn't get the expected result. 然后,我尝试了一些代码,如果它在第一个数据库中找不到数据,则移至第二个数据库,依此类推,直到找到数据为止,否则“在数据库中不打印数据”,但是我没有得到预期的结果。 I am new to php so have not much idea. 我是php新手,所以没有太多想法。 So any help is highly appreciated and Thank you in advance. 因此,我们非常感谢您的帮助,并在此先感谢您。 My code is given below. 我的代码如下。

<?php

class FindData{
    public $DatabaseDetails=array(
        array('DatabaseName'=>'database1','TableName'=>'table1'),
        array('DatabaseName'=>'database2','TableName'=>'table2'),
        array('DatabaseName'=>'database3','TableName'=>'table3')
    );

    public function SqlData(){
        $i=0;
        $count=count($this->DatabaseDetails);
        $email="ileee@example.com";
        $con=mysqli_connect('localhost','root','',$this->DatabaseDetails[$i]['DatabaseName']);
        $sql="select * from ".$this->DatabaseDetails[$i]['TableName']." where email='".$email."'"
        ;
        $run=$this->Data(mysqli_query($con,$sql));
        if(strpos($run,'No data')!==false){
            $i+=1;
        }else{
            echo $run;
        }
    }

    public function Data($run){
        if($run){
            $num=mysqli_num_rows($run);
            if($num>=1){
                return "Data is in presented"; 
            }else{
                return "No data";
            }
        }
    }

}
$obj=new FindData();
$obj->SqlData(); 

?> ?>

You are not looping over the databases 您没有遍历数据库

public function SqlData(){
    $i=0;
    $count=count($this->DatabaseDetails);
    $email="ileee@example.com";
    $con=mysqli_connect('localhost','root','',$this->DatabaseDetails[$i]['DatabaseName']);
    $sql="select * from ".$this->DatabaseDetails[$i]['TableName']." where email='".$email."'"
    ;
    $run=$this->Data(mysqli_query($con,$sql));
    if(strpos($run,'No data')!==false){
        $i+=1;
    }else{
        echo $run;
    }
}

Even if you call this function multiple times externally, you are not changing the $i value. 即使您在外部多次调用此函数,也不会更改$i值。 So you only use the $i=0 database. 因此,您仅使用$i=0数据库。

You try to update it here 您尝试在此处更新

if(strpos($run,'No data')!==false){
    $i+=1;
}

But because there is no loop in the method, and it's a local value. 但是因为该方法中没有循环,所以它是一个局部值。 There is either no iteration done or the value of $i is reset to 0 on each call to the method. 每次都没有迭代完成,或者$i的值重置为0 As I said it's unclear if the method is called once or multiple times to provide the iteration. 正如我所说的,尚不清楚是一次还是多次调用该方法以提供迭代。 In either case it doesn't matter. 无论哪种情况都没有关系。

So there are 3 ways to fix this. 因此,有3种方法可以解决此问题。

  • add a loop inside the method 在方法内部添加循环
  • pass in the value of $i as an argument (multiple external calls) 传入$i的值作为参数(多个外部调用)
  • pass in the value of $i as an argument (recursive call) 传入$i的值作为参数(递归调用)

Make sense. 说得通。

I solved the issue. 我解决了这个问题。 Thanks, everyone, for all the help and suggestions. 谢谢大家的帮助和建议。 Below is my code: 下面是我的代码:

<?php
class FindData{
    public $DatabaseDetails=array(
        array('DatabaseName'=>'database1','TableName'=>'table1'),
        array('DatabaseName'=>'database2','TableName'=>'table2'),
        array('DatabaseName'=>'database3','TableName'=>'table3')
    );

    public function SqlData(){      
        $email="patelbhai16@gmail.com";
        $count=count($this->DatabaseDetails);
        $i=0;
        foreach($this->DatabaseDetails as $key =>$value){
            $con=mysqli_connect('localhost','root','',$value['DatabaseName']);
            if($con){
                $sql="select * from ".$value['TableName']." where email='".$email."'";
                $run=$this->Data(mysqli_query($con,$sql));
                if(strpos($run,'No data')!==false){
                    $i++;
                    if($i==$count){
                        if(strpos($run,'No data')!==false){
                            echo "No data";
                        }
                    }
                }else{
                    echo $run;
                    break;
                }
            }
        }       

    }

    public function Data($run){
        if($run){
            $num=mysqli_num_rows($run);
            if($num>=1){
                return "Data is in presented"; 
            }else{
                return "No data";
            }
        }
    }

}
$obj=new FindData();
$obj->SqlData(); 
?>

it may help someone in future. 将来可能会对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM