0

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. 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="[email protected]";
        $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(); 

?>

4
  • What you are getting now Commented Mar 31, 2018 at 17:43
  • array('DatabaseName'=>'database2','TableName'=>'table2'), here you given two quotes in your code check that Commented Mar 31, 2018 at 17:47
  • That is a mistake when i am putting the code here.The main code is given below.Any help is highly appreciated. If I am checking for st database data it is showing fine but is not lopping for other database in array Commented Mar 31, 2018 at 17:52
  • Sorry, I had to put the smack down on it. :) Commented Mar 31, 2018 at 18:27

2 Answers 2

1

You are not looping over the databases

public function SqlData(){
    $i=0;
    $count=count($this->DatabaseDetails);
    $email="[email protected]";
    $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. So you only use the $i=0 database.

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. 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.

  • add a loop inside the method
  • pass in the value of $i as an argument (multiple external calls)
  • pass in the value of $i as an argument (recursive call)

Make sense.

Sign up to request clarification or add additional context in comments.

Comments

0

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="[email protected]";
        $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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.