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));
Related
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();
Is this possible to be done with SQL?
I need to make a SQL selection depending if a $query is false WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" ELSE "$query";
That's my starting point where both conditions have to be met:
$validatedSearchData = array(
"q"=>strip_tags($_GET["q"])
);
$query= " AND a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
$feed = lib::$db->GetAll("SELECT SQL_CALC_FOUND_ROWS
a.*,
u.name,
fu.id_user AS fu_user_id,
ff.id_followed_user AS ff_user_id
FROM feed AS a
LEFT JOIN userfollow AS fu ON a.id_author = fu.id_user
LEFT JOIN userfollow AS ff ON a.id_author = ff.id_followed_user
INNER JOIN user_profiles AS u ON a.id_author = u.id_user
WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" . $query. "
GROUP BY a.id_article
");
Change this line
$query= " OR a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
replace OR in the place of AND
I have a MySql Query that is not returning correct values in PHP, but if I run the same MySql Query in phpMyAdmin, it returns a value. If I display the select in a web browser, I get a 'Resource id #27' on the end of it.
PHP Code
$SQL_PhotoQueryList = "SELECT count(*) FROM `invoice_detail`".
" INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id".
" INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id".
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
$SQL_PhotoResultList = mysql_query($SQL_PhotoQueryList);
$ListPhotoCount = mysql_result($SQL_PhotoResultList,0);
echo "SQL Query = $SQL_PhotoQueryList<br>";
echo "ListCount = $ListPhotoCount<br>";
Screen Output
SQL Query = SELECT count(*) FROM `invoice_detail` INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id WHERE invoice.invoice_active = '2' AND photos.user_id = '2'Resource id #27
ListCount = 0
Code calling the routine ($SessionUserID is a $_SESSION Variable)
$PassStatus = "2"; // Active
require("get_invoice.php");
$InfoTotalSales = $ListGalleryCount;
It looks like you have a typo.
$SQL_PhotoQueryList = "SELECT count(*) FROM `invoice_detail`".
" INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id".
" INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id".
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
$SQL_PhotoResultList = mysql_query($SQL_PhotoQueryList);
$ListPhotoCount = mysql_result($SQL_PhotoResultList,0);
echo "SQL Query = $SQL_PhotoQueryList<br>";
echo "ListCount = $ListPhotoCount<br>";
Note the full stop (actually, concatenation operator) on the last line of the query:
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
This should be a semicolon. Yep, I've done that too. Sometimes the hardest mistake to find.
The resource ID at the end is from the result of mysql_query().
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 < ?
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}