Results different when running query in phpmyadmin and on website - php

I have two tables which I'm trying to run a query on.
If I run the below query in phpmyadmin I get the results I expect, just one result returned with id 9, but when running it on my webpage, its returning all 4 results. I can put what ever I want as the user id, even one that doesn't exist, its ignoring it.
$sql = "
SELECT c.*
, cu.userID
FROM ConversationUsers AS cu
LEFT
JOIN Conversations AS c
ON c.id = cu.convoID
WHERE cu.userID = userID
";
$q = $pdo->prepare($sql);
$q->bindValue(':userID', $vars['userID']);
$q->execute();
if($q->errorCode() != 0) {
$errors = $q->errorInfo();
echo($errors[2]);
}
foreach ($q->fetchAll() as $row) {
$convos[] = $row;
}
echo "<pre>";
print_r($convos);
echo "</pre>";
echo "
SELECT c.*
, cu.userID
FROM ConversationUsers AS cu
LEFT
JOIN Conversations AS c
ON c.id = cu.convoID
WHERE cu.userID = " . $vars["userID"];
The echo produces:
SELECT c.*
, cu.userID
FROM ConversationUsers AS cu
LEFT
JOIN Conversations AS c
ON c.id = cu.convoID
WHERE cu.userID = 1
The print produces
Array
(
[0] => Array
(
[id] => 9
[0] => 9
[lastUpdated] => 2019-12-29 00:00:00
[1] => 2019-12-29 00:00:00
[userID] => 1
[2] => 1
)
[1] => Array
(
[id] => 9
[0] => 9
[lastUpdated] => 2019-12-29 00:00:00
[1] => 2019-12-29 00:00:00
[userID] => 2
[2] => 2
)
[2] => Array
(
[id] => 10
[0] => 10
[lastUpdated] => 2019-12-29 00:00:00
[1] => 2019-12-29 00:00:00
[userID] => 2
[2] => 2
)
[3] => Array
(
[id] => 10
[0] => 10
[lastUpdated] => 2019-12-29 00:00:00
[1] => 2019-12-29 00:00:00
[userID] => 3
[2] => 3
)
)
The tables are setup as follows
CREATE TABLE `Conversations` (
`id` int(11) NOT NULL,
`lastUpdated` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Conversations` (`id`, `lastUpdated`) VALUES
(9, '2019-12-29 00:00:00'),
(10, '2019-12-29 00:00:00');
CREATE TABLE `ConversationUsers` (
`convoID` int(11) NOT NULL,
`userID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `ConversationUsers` (`convoID`, `userID`) VALUES
(9, 1),
(9, 2),
(10, 2),
(10, 3);

You're missing the : in :userID. That makes the WHERE always be true:
$sql = "SELECT c.*, cu.userID FROM ConversationUsers AS cu LEFT JOIN Conversations AS c ON c.id = cu.convoID WHERE cu.userID = :userID";
Your echo line used $vars["userID"] which is why it showed the correct result.

You miss the : before the parameter in your query:
$sql = "SELECT c.*, cu.userID FROM ConversationUsers AS cu LEFT JOIN Conversations AS c ON c.id = cu.convoID
WHERE cu.userID = :userID";

Related

how to create structure with data repeated in a certain period

I need to return data from two tables this way
Array(
[0] => Array(
[Name] => lorem,
[Qty] => 2),
[1] => Array(
[Name] => ipsum,
[Qty] => 1),
[2] => Array(
[Name] => dolor,
[Qty] => 0)
)
PHP will only take care of getting the data, there will be no manipulation to organize it.
Two tables, one to record a certain event, the date and the id of a table related to the event. And the tables are like this:
tableA
id | date | tableB_id
1 2020-10-02 2
1 2020-10-19 2
1 2020-10-21 1
1 2020-11-2 3
1 2020-11-11 1
tableB
id | name
1 lorem
2 ipsum
3 dolor
And my SQL query
SELECT b.name as Name, a.created_at as created
FROM tableA b
INNER JOIN tableA a ON b.tableA_id = a.id WHERE MONTH(b.created_at) = '10' ORDER BY a.id;
which results in
Name | created
ipsum 2020-10-02
ipsum 2020-10-19
lorem 2020-10-21
You can Left Join TableA, but it irst need to have all the wanted rows as a subquery, to get also the 0 count
CREATE TABLE tableA (
`id` int,
`date` date,
`tableB_id` int
);
INSERT INTO tableA
(`id`, `date`, `tableB_id`)
VALUES
('1', '2020-10-02' , '2'),
('1' , '2020-10-19' , '2'),
('1' , '2020-10-21' , '1'),
('1' , '2020-11-02' , '3'),
('1' , '2020-11-11', '1');
CREATE TABLE tableB (
`id` int,
`name` varchar(19)
);
INSERT INTO tableB
(`id`, `name`)
VALUES
('1', 'lorem'),
('2', 'ipsum'),
('3', 'dolor');
SELECT b.`name`, Count(`tableB_id`)
FROM tableB b LEFT JOIN (SELECT * FROM tableA WHERE MONTH(`date`) = '10') a ON a.tableB_id = b.id
GROUP BY b.name
;
name | Count(`tableB_id`)
:---- | -----------------:
lorem | 1
ipsum | 2
dolor | 0
db<>fiddle here
You can use next php code:
$query = "SELECT
name Name, COUNT(tableA.id) Qty
FROM tableB
LEFT JOIN tableA ON tableB.id = tableA.tableB_id AND MONTH(tableA.date) = 10
GROUP BY tableB.name;";
// get DB version using PDO
$stmt = $pdo->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);
Live PHP fiddle
Result:
Array
(
[0] => Array
(
[Name] => dolor
[Qty] => 0
)
[1] => Array
(
[Name] => ipsum
[Qty] => 2
)
[2] => Array
(
[Name] => lorem
[Qty] => 1
)
)

MySQL join if address value not equal default

Data:
[ID] => 4
[Create_date] => 2017-10-07
[Client] => 17
[Address] => default
[GST] => Regular
[Rate_group] => D
[Sales_man] => 0
[CGST] => 91.12
[SGST] => 91.12
[IGST] => 0
[Discount] => 10
[Total] => 1397.25
[LR_number] => 0
[LR_date] => 0000-00-00
[Transport] => 5
[Transport_branch] => default
[Remark] => Hi, my name is Rushabh Shah.
[Payment_mode] => Cash
[Status] => Open
[Customer_name] => Rushabh Infotech
[Transport_name] => Shah Infotech
MYSQL Query:
SELECT `sale`.*,
`customer`.`english_company_name` AS `Customer_name`,
`transport`.`english_company_name` AS `Transport_name`
FROM `sale`
LEFT JOIN `customer`
ON `customer`.`id` = `sale`.`client`
LEFT JOIN `transport`
ON `transport`.`id` = `sale`.`transport`
MySQL join with Transport_branch if Transport_branch != 'default'. Can anyone please tell me how can I achieve this.
You could use a case when
SELECT `sale`.*,
`customer`.`english_company_name` AS `Customer_name`,
`transport`.`english_company_name` AS `Transport_name` ,
case when `transport`.`transport_branch` = 'default'
THEN `transport`.`english_company_name`
ELSE `transport_brach`.`transport_btanch__company_name`
end AS my_transport_name
FROM `sale`
LEFT JOIN `customer`
ON `customer`.`id` = `sale`.`client`
LEFT JOIN `transport`
ON `transport`.`id` = `sale`.`transport`
LEFT JOIN `transport_branch`
ON `transport_branch`.`id` = `transport`.`id`

optimize looping mysql result codeigniter mysql php

Get the results from follow table max(date)
leads table
id name
75943 name1
82501 name2
lead_follow_up
id lead_id msg date updated
1 75943 msg1 1438930800 1438884890
2 75943 msg2 1416459600 1415901523
3 82501 msg1 1454713200 1454485087
4 82501 msg1 1439362800 1438564891
using the following query i get result
$today_date = mktime(0, 0, 0, $mon, $day-1, $year);
SELECT * FROM (`lead_follow_up`) LEFT JOIN `leads` ON `leads`.`id` = `lead_follow_up`.`lead_id` WHERE `date` <= $today_date GROUP BY `lead_follow_up`.`lead_id` ORDER BY `lead_follow_up`.`date` DESC
from the above query i get array $previou
$previou= Array
(
[0] => stdClass Object
(
[id] => 1
[lead_id] => 75943
[date] => 1438930800
[updated_on] => 1438884890
)
[1] => stdClass Object
(
[id] => 2
[lead_id] => 75943
[date] => 1416459600
[updated_on] => 1415901523
),
[2] => stdClass Object
(
[id] => 3
[lead_id] => 75943
[date] => 1416459600
[updated_on] => 1415901523
),....etc
);
foreach($previou as $key => $p):
$q = "SELECT `id` FROM (`lead_follow_up`) WHERE `lead_id` = '".$p->id."' AND `date` > '".$p->date."' ORDER BY `updated_on` DESC ";
if(!$this->db->query($q)){
$previouData[$key] = $p;
$pCount++;
}
endforeach;
What I'm really expecting to see is data like
id lead_id name msg
1 75943 name1 msg1
any help to optimize this in single query

How to select Distinct on inner join?

Can help me, I have something problem about query select distinct, :
My table
table_pemesan
id_pms
table_pesan
no_psn
id_pms
table_dpesan
no_psn
My query mysql
select pm.id_pms,
pm.nama,
pm.date_create,
ps.no_psn,
ps.status,
dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC
//output
Array
(
[0] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 1
)
[1] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 2
)
)
the array index 0 & 1 the same data, and my question how to distinct the query? thanks b4, sorry my english not good..
Try to use this query:-
SELECT DISTINCT pm.id_pms, pm.nama, pm.date_create, ps.no_psn, ps.status, dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC

Output concated value from mysql query

I am trying to output a concated value from my query but am having some issues.
Here is my query:
$result = mysql_query(
"SELECT c.company, n.nid, n.createdOn, CONCAT_WS(' ',c2.fname,c2.lname), CONCAT_WS(' ',c3.fname,c3.lname), n.urgent, n.description
FROM notes n
INNER JOIN Positions p ON FIND_IN_SET(p.id, n.forDepts) > 0
LEFT JOIN companies c ON c.userid = n.clientId
LEFT JOIN companies c2 ON c2.userid = n.createdBy
LEFT JOIN companies c3 ON c3.userid = n.claimedBy
GROUP BY n.nid
LIMIT 0,100"
);
My array is printing like so:
Array ( [0] => Honda of Kirkland [company] => Honda of Kirkland [1] => 1 [nid] => 1 [2] => 2009-09-28 21:33:15 [createdOn] => 2009-09-28 21:33:15 [3] => [CONCAT_WS(' ',c2.fname,c2.lname)] => [4] => [CONCAT_WS(' ',c3.fname,c3.lname)] => [5] => 0 [urgent] => 0 [6] => Milestones [description] => Milestones )
I am trying it like this, but it does not work:
while ($row = mysql_fetch_array($result)) {
$created_by = $row[3];
}

Categories