Reputation: 13979
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();
?>
Upvotes: 0
Views: 607
Reputation: 13979
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.
Upvotes: 0
Reputation: 21661
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.
$i
as an argument (multiple external calls)$i
as an argument (recursive call)Make sense.
Upvotes: 1