John Davidson

php - join 2 query from different database

0 comments
Message:


Hi is this the right way to join 2 query from different database? or there is no way to join 2 database in one 1 function


Model.php


public function getUserGroup($userId = null) 
{
if($userId) {
$sql = "SELECT * FROM user_group WHERE user_id = ?";
$query = $this->db->query($sql, array($userId));
$result = $query->row_array();

$group_id = $result['group_id'];
$g_sql = "SELECT * FROM groups WHERE id = ?";
$g_query = $this->db->query($g_sql, array($group_id));

$store_id = $result['store_id'];
$s_sql = "SELECT * FROM stores WHERE id = ?";
$s_query = $this->db->query($s_query, array($store_id));
$s_result = $s_query->row_array();

$t_result= $g_query ->row_array(), $s_result->row_array();
return $t_result;
}
}



My Answer:
"SELECT * FROM database1.users WHERE id = $userId;"; $result1 = $this->db1->query($sql); $user = $result1->fetch_assoc(); $groupId = $user['group_id']; $sql2 = "SELECT * FROM database2.groups WHERE id = $groupId;"; $result2 = $this->db2->query($sql2); $group = $result2->fetch_assoc(); return $group; } else { return false; } }

Rate this post

4 of 5 based on 1953 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us