Mysql query failed to execute - php

Why this code does not work in the database?
$sql =
UPDATE users_information INNER JOIN users ON users.id = user_id SET following_count = :following_count WHERE user_id = :user_id;
$data :
["following_count" => "following_count + 1" , "user_id" => "1273"]
execute return false

following_count + 1 is expression, You cannot bind expressions, only values. So, rewrite your query as:
$sql = "
UPDATE users_information INNER JOIN users ON users.id = user_id
SET following_count = following_count + 1
WHERE user_id = :user_id";
And in $data you pass only user_id:
$data = ["user_id" => "1273"];
Also, it's unclear to me what's the point of using join, why not simple query:
$sql = "
UPDATE users_information
SET following_count = following_count + 1
WHERE user_id = :user_id";

["following_count" => "following_count + 1" , "user_id" => "1273"] .
Here "following_count + 1" its a string .
Make it Integer. Or if its a string then in database change the type
as varchar.
data should be like the following:
$following_count = $following_count + 1;
["following_count" => "some_number(from following_count value)" , "user_id" => "1273"]

Related

How to update table field = field+1 in laravel eloquent update query

I want to update a query in laravel where $field = $field+1 . It works fine using mysqli_query. But It is not working laravel orm.
I want to run this query in laravel orm
$sql = "Update $tbl set cnt=cnt+1 where user_id='x'"
When running this query in mysql I get following results
If cnt =1 in databases it will increment $cnt = $cnt+1 = 1+1 =2
I want to run this sql query in laravel orm
$where = ['user_id' => 'x'];
$field = ['cnt' => 'cnt+1'];
$tbl = 'log_cnt';
Capsule::table($tbl)->where($where)->update($field);
But After running this query output will show $cnt=0 in database
Incrementing or decrementing a value of a column
To increment single column
Capsule::table('log_cnt')->increment('cnt');
Query: UPDATE log_cnt SET cnt = cnt + 1;
Capsule::table('log_cnt')->increment('cnt', 5);
Query: UPDATE log_cnt SET cnt = cnt + 5;
To decrement single column
Capsule::table('log_cnt')->decrement('cnt');
Query: UPDATE log_cnt SET cnt = cnt - 1;
Capsule::table('log_cnt')->decrement('cnt', 5);
Query: UPDATE log_cnt SET cnt = cnt - 5;
You may also specify additional columns to update:
Capsule::table('log_cnt')->increment('cnt', 1, ['user_id' => 'x']);
Query: UPDATE log_cnt SET cnt = cnt + 1, user_id = 'x';
Reference : https://laravel.com/docs/5.4/queries#increment-and-decrement

Assigning the same parameter value multiple times in pdo execute

I originally had an SQL statement, this:
SELECT *, COUNT(friend_one) AS pending_count , COUNT(friend_two) AS requests_sent
FROM friends
WHERE friend_one OR friend_two = ?
AND status = ?
In which I assigned my parameters like :
$pending_friend_count_stmt->execute(array($user_id, $status_one));
However, the query was not getting the results I wanted. Someone showed me a different way of doing it, but it has the variable $user_id in it multiple times, so I do not know how to adjust the code to be able to use a parameter.
You can see the new query here:
http://rextester.com/KSM73595
Am I able to just do
SELECT COUNT(CASE WHEN `friend_one` = ? THEN 1 END) as `requests_count`,
COUNT(CASE WHEN `friend_two` = ? THEN 1 END) as `pending_count`
FROM `friends`
WHERE ? IN ( `friend_one` , `friend_two` )
AND `status` = ?
$pending_friend_count_stmt->execute(array($user_id, $user_id, $user_id $status_one));
Using PDO you have the ability to use named parameters, however in your question you want to use 1 parameters for multiple values and that means emulation has to be on:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Now you can do the following:
$stmt = $db->prepare("SELECT * FROM table WHERE userid = :userid AND userid = :userid");
$stmt->excecute([
':userid' => 1
]);
Resulting in:
"SELECT * FROM table WHERE userid = 1 AND userid = 1"

Copy data from one MySQL table to another when data intersect using PHP

I'm new to programming and would really appreciate your help. :) So, I have a USER table and a SALES table. On the SALES table I only have name and last name of the users. On the USER table I have name, last name, USER_ID, email and etc...
I need to copy the USER_ID from the USER table to the SALES table when NAME and LAST NAME are a match.
Here is the structure:
USER_TABLE_A
USER_ID_A
NAME_LASTNAME_A
SALES_TABLE_B
ROW_ID_B
NAME_B
LASTNAME_B
USER_ID_B (empty)
So far I got both tables to show data when they intersect but have no idea where to go from here. Could anyone please help?
$sql1 = mysql_query("SELECT name_B, lastname_B, user_id_B, row_id_B FROM sales_table_B WHERE name_B IS NOT NULL AND lastname_B IS NOT NULL", $db);
$sql2 = mysql_query("SELECT name_lastname_A, user_id_A FROM user_table_A WHERE name_lastname_A IS NOT NULL", $db);
$a1 = array();
while ($row = mysql_fetch_array($sql1)) {
$id = $row['row_id_B'];
$name1.$id = $row['name_B']." ".$row['lastname_B'];
array_push($a1, $name1.$id);
}
$a2 = array();
while ($row2 = mysql_fetch_array($sql2)) {
$id2 = $row2['user_id_A'];
$name2.$id2 = $row2['name_lastname_A'];
array_push($a2, $name2.$id2);
}
$result = array_intersect($a1,$a2);
print_r($result);
Thanks in Advance!
Under the assumption that NAME_LASTNAME_A column in USER_TABLE_A is a concatenation of NAME_B and LASTNAME_B columns of SALES_TABLE_B, following UPDATE query should update the ids:
update sales_table_b
set b.user_id_b = (
select user_id_a from user_table_a
where name_lastname_a = concat(b.name_b, ' ' , b.lastname_b)
limit 1
where exists(
select user_id_a from user_table_a
where name_lastname_a = concat(b.name_b, ' ' , b.lastname_b)
)
)
Please note that in case of multiple users having same first and last name, id of the first matching user will be considered.
Thank you so much Darshan!! Your answer was missing a ) after limit 1 but with the adjustment it worked beautifully! Here is the code that worked:
UPDATE sales_table_b
SET user_id_b = (SELECT user_table_a.user_id_a
FROM user_table_a
WHERE user_table_a.name_lastname_a = CONCAT(sales_table_b.name_b, ' ' , sales_table_b.lastname_b) LIMIT 1)
WHERE EXISTS (SELECT *
FROM user_table_a
WHERE user_table_a.name_lastname_a = CONCAT(sales_table_b.name_b, ' ' , sales_table_b.lastname_b))

get value from subquery in CodeIgniter

the thing is that i don know if there is a way to get a value from a subquery over an update operation, the query that i'm trying to do looks like this, on a example code of my model :
$query = "UPDATE some_table SET value_1 = ((SELECT value_2 FROM other_table WHERE id = 2) + 1) WHERE id = 2";
$this->db->query($query);
i hope you can help me or at least let me know an alternative way to get this using the active record
PD i'm working with the CodeIgniter Framework
The equivalent of
UPDATE some_table SET value_1 = ((SELECT value_2 FROM other_table WHERE id = 2) + 1) WHERE id = 2
in ActiveRecord is probably:
$value_2 = $this->db->get_where('other_table', array('id' => 2))[0];
$data = array('value_1', $value_2 + 1);
$this->db->update('some_table', $data, array('id' => 2));
I not working with Codeigniter, but try this:
$query = "UPDATE some_table st SET st.value_1 = ((SELECT ot.value_2 FROM other_table ot WHERE ot.id = 2) + 1) WHERE st.id = 2";
$this->db->query($query);

Using 'AND' and 'OR' in same mysql statement with PDO

Is it possible to do this?
Lets say I have a table : data
$id_1 = "checking";
$id_2 = "box";
$id_users = 1;
id id_1 id_2 id_users
1 checking box 1
2 checking circle 1
3 box checking 1
4 box checking 1
$sql = $db->prepare("SELECT COUNT(*) FROM data WHERE ((id_1 = ? AND id_2= ?) OR (id_1 = ? AND id_2 = ?)) AND id_users = ?");
$sql -> execute(array($id_1, $id_2, $id_2, $id_1, $id_users));
echo count($sql);
With this, I'm getting an output of 1 only. Technically I should be getting an output of 3, correct? Because there are 3 possibilities with checking and box.
The SQL is supposed to check either table for the two combinations of checking and box.
Can someone tell me what I'm doing wrong here?
Thanks
It looks like you're count()ing the already-counted SQL COUNTed number of rows.
How about just echo $sql?
$sql = $db->prepare("SELECT COUNT(*) FROM data WHERE ((id_1 = ? AND id_2= ?) OR (id_1 = ? AND id_2)) AND id_users = ?");
$sql->execute(array($id_1, $id_2, $id_2, $id_1, $id_users));
echo $sql->fetch();
What MДΓΓ БДLL said is ok, but you could also use named parameters:
$sql = $db->prepare("SELECT COUNT(*) FROM data WHERE ((id_1 = :id1 AND id_2= :id2) OR (id_1 = :id2 AND id_2 = :id1)) AND id_users = :idusers");
$sql -> execute(array(':id1' => $id_1, ':id2' => $id_2, ':idusers' => $id_users));
And you need to fetch result
$result = $sql->fetch();
echo $result[0];
In your query it seems that you are passing id_1 as parameter to id_2 and id_2 parameter as id_1 so as per your query there is only one combination of id_1 = checking and id_2 = box ,
so you are getting output count as 1
instead of this to avoid parameter confusion use
$sql -> execute(array(':id1' => $id_1, ':id2' => $id_2, ':idusers' => $id_users));

Categories