join 2 query from different database - php

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

you can join table in mysql query is below demo link
CREATE TABLE user_group (user_id INT,group_id INT,store_id INT);
INSERT INTO user_group VALUES (1,1,1);
INSERT INTO user_group VALUES (1,1,2);
INSERT INTO user_group VALUES (2,1,1);
INSERT INTO user_group VALUES (2,1,2);
-- fetch
SELECT * FROM user_group;
CREATE TABLE `groups` (group_id INT,group_name varchar(255));
INSERT INTO `groups` VALUES (1,'group-1');
INSERT INTO `groups` VALUES (2,'group-2');
SELECT * FROM `groups`;
CREATE TABLE stores (store_id INT,store_name varchar(100));
INSERT INTO stores VALUES (1,'store-1');
INSERT INTO stores VALUES (2,'store-2');
SELECT * FROM stores;
SELECT * FROM user_group ug
JOIN `groups` g on g.group_id = ug.group_id
JOIN stores s on s.store_id = ug.store_id
WHERE ug.user_id = 1;
and you can use codeigniter(CI)query like below and for your reference link
$this->db->select('*');
$this->db->from('user_group');
$this->db->join('groups', 'groups.group_id = user_group.group_id');
$this->db->join('stores', 'stores.store_id = user_group.store_id');
$this->db->where('user_group.user_id', 1); // <--- "1" = that is your's $userId
$query = $this->db->get();

Related

PHP select doesn't return values

I have this code and I want to get the info in medication table and display it where acc_id in account table is = to acc_id in medication table and where med_timeoftheday='morning'
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$User_ID = $request->acccid;
$sql = sprintf("SELECT * FROM account_info
join medication on account_info.acc_id = medication.acc_id
where account_info.acc_id='%s'",
mysqli_real_escape_string($conn,$User_ID));
$result=$conn->query($sql);
if ($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{$data[]=$row;
}
echo json_encode($data);
}
}
this is my ts :
how can I do that ?
Thank you in advance!
Try somehting like this:
SELECT * FROM medication
INNER JOIN account_info ON account_info.acc_id = medication.acc_id
WHERE medication.med_timeoftheday='morning'
firstly if you selected data from medication table so select first medication table and then using join with account table .
$sql = "SELECT * FROM medication JOIN account_info ON account_info.acc_id = medication.acc_id WHERE medication.med_timeoftheday='morning'";

how to select data from a table useing values from another table

I want to select data from a table called tbl_users using a value from another table called mergeing and column called donator_1.
I tried the following:
$result = $DBcon->query("SELECT tbl_users.username, tbl_users.email, tbl_users.Phone_number FROM tbl_users,mergeing WHERE mergeing.donator_1= tbl_users.user_id AND mergeing._id = 6");
while($row = $result->fetch_array()) {
echo '<b>' . $l['user_id'] .'</b><b>' . $l['username'] . '</b>';
}
You can try nested queries:
SELECT username, email, Phone_number
FROM tbl_users
WHERE user_id = (SELECT donator_1 FROM mergeing WHERE _id = 6 )
or an inner join :
SELECT username, email, Phone_number
FROM tbl_users
JOIN mergeing ON tbl_users.user_id = mergeing.donator_1
WHERE mergeing._id = 6

Cannot store SELECT query into PHP variable

I'm trying to store a query result in order to use it in another SELECT statement but it isn't working..
$username = $_SESSION['username'];
$result = "SELECT sensorid FROM users WHERE username = '$username' ";
$result is supposed to have an integer but how can I use that variable into another select like...
$sql = "SELECT * FROM sensor WHERE sensorid = '$result'";
You need to join users table with sensor table on sensorid column.
$query = "select s.* from users u join sensor s on s.id = u.sensorid where u.username = $username"
See this

UPDATE inner join query with pdo

i am trying to update two tables with one query using inner join but it's not updating or neither showing any error. here is the code
$id_prod = 2;
$id_cust = 2;
$sql5 = "UPDATE `customer`
INNER JOIN `products` ON products.cust_id=customer.id
SET prod_name = 'CAKE' AND name = 'Hassan'
WHERE id='$id_cust' AND id='id_prod' ";
$query5 = $conn->prepare($sql5);
$query5->execute(array($id_cust, $id_prod));
Maybe the column to be set is misnamed, if you prefix the column name with the table name, is that better ?
Like this :
$id_prod = 2;
$id_cust = 2;
$sql5 = "UPDATE `customer`
INNER JOIN `products` ON products.cust_id=customer.id
SET __tableName__.prod_name = 'CAKE' AND __tableName__.name = 'Hassan'
WHERE __tableName__.id='$id_cust' AND __tableName__.id='id_prod' ";
$query5 = $conn->prepare($sql5);
$query5->execute(array($id_cust, $id_prod));

How To Avoid Queries in loops

Hi how can i avoid queries like these?
sample query:
$sql = DB::getInstance()->prepare("SELECT tb_id1 FROM table1 WHERE duedate < ?");
$sql->execute(array($currentdate));
if($sql->rowCount()){
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$sql2 = DB::getInstance()->prepare("UPDATE table2 SET isOverdue = 1 WHERE tb_id2 = ?");
$sql2->execute(array($row["tb_id1"]));
}
}
You can use update with join and thus by not using any loop in PHP
update table2 t2
join table1 t1 on t1.tb_id1 = t2.tb_id2
set t2.isOverdue = 1
where
t1.duedate < ?

Categories