Yii2 issue to get the result for db query - php

I am using this command to get if the record already exist in the database;
$query = Yii::$app->db->createCommand("SELECT IF(EXISTS(SELECT * FROM `order_item`
WHERE `date` = '$date' AND `start_time` = '$starttime'), 1, 0)");
$result=$query->queryAll();
var_dump($result);exit;
Now the result I am getting for dump is like:
array(1) { [0]=> array(1) { ["IF(EXISTS(SELECT * FROM `order_item`
WHERE `date` = '2018-12-03' AND `start_time` = '10:15:00'), 1, 0)"]=> string(1) "0" } }
whereas I want the result as just 1 or 0
like
if ($result==1){
//do something;
}
as if I am running the same query in phpmyadmin - I am getting the result as 0 or 1
How I can achieve the same from the Query in Yii2.

queryAll() returns all fields from all rows as arrays. If you want to get single value from first field of first row, you need to use queryScalar().
$result = $query->queryScalar();

Related

How to add every last record on the database? E.g. 1 += 1

So, Im fetching the last record on the table "energy"
function find_all_energy() {
global $db;
$sql = "SELECT * FROM energy ORDER BY energyID DESC";
$result = mysqli_query($db, $sql);
$energy = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $energy;
}
The last record returns "1000", but what if there are new inserted record(s) like "2000", I want to return it as "3000" because of ( += ) and new inserted record(s) again like "5000" then it will fetch it as "8000" and so on. Thank you!
What you what is the last line of your table ?
I would suggest to do it with a MAX(id) on a WHERE requirement, like it is suggested in this post :
SELECT row
FROM table
WHERE id=(
SELECT max(id) FROM table
)

Varchar values transform into 0 in php mysql query

I have a MySQL database containing tables with Arabic data (Varchar type exactly), I try to print a select query string thought php but it prints the values as zeros, Here's my query :
SELECT *,(
CASE WHEN `name_singular`= "قَائِدان" THEN "name_singular"
WHEN `name_twoA`= "قَائِدان" THEN "name_twoA"
WHEN `name_pluralA`= "قَائِدان" THEN "name_pluralA"
WHEN `name_twoO`= "قَائِدان" THEN "name_twoO"
WHEN `name_pluralO`= "قَائِدان" THEN
"name_pluralO" else NULL END) AS `field`
FROM `name` WHERE `name_singular`= "قَائِدان" OR
`name_twoA`= "قَائِدان" OR
`name_pluralA`= "قَائِدان" OR
`name_twoO`= "قَائِدان" OR
`name_pluralO`= "قَائِدان" LIMIT 0, 10 ;
and that's what i got as a result of printing the query string.
it works fine when executing it thought mysql directly, but when using php it returns null
SELECT *,
( CASE
WHEN `name_singular` = "0" THEN "name_singular"
WHEN `name_twoa` = "0" THEN "name_twoa"
WHEN `name_plurala` = "0" THEN "name_plurala"
WHEN `name_twoo` = "0" THEN "name_twoo"
WHEN `name_pluralo` = "0" THEN "name_pluralo"
ELSE NULL
end ) AS `field`
FROM `name`
WHERE `name_singular` = "0"
OR `name_twoa` = "0"
OR `name_plurala` = "0"
OR `name_twoo` = "0"
OR `name_pluralo` = "0"
LIMIT 0, 10
And that's the php code :
<?php $query = 'the query string above ';
echo $query; $array=mysql_fetch_object(mysql_query($query));
var_dump($array);
?>

SQL query is returning the wrong results

At the top of my file I have checked what the $clientid is using echo $clientid. This echoes 731.
Further down the script I'm running this query:
$active_ts = db_select("SELECT * FROM `timesheets` WHERE clientid='".$clientid."' AND status=\"client\" OR status=\"cand\"");
var_dump($active_ts);
What I expect to happen is only timesheets with a clientid of 731 to be chosen.
What the var_dump shows however is ["clientid"]=> string(3) "345", ["clientid"]=> string(3) "712", ["candid"]=> string(3) "730" and ["clientid"]=> string(3) "721"
I don't really know how to debug this further....
This is your query:
SELECT *
FROM `timesheets`
WHERE clientid = '".$clientid."' AND status = 'client' OR status = 'cand'
The WHERE clause has no parentheses. It is interpreted as:
WHERE (clientid = '".$clientid."' AND status = 'client') OR status = 'cand'
This is presumably not what you want. I would suggest you use IN:
WHERE clientid = '".$clientid."' AND status IN ('client', 'cand')
Because query select user with status = cand.
You need to change your query :
Change :
$active_ts = db_select('
SELECT * FROM `timesheets`
WHERE clientid='.$clientid.' AND (status="client" OR status="cand")
');
I think you don't need ' between clientid because it's int.

How retrieve data from a 2D array on this particular case?

I have a array with this var_dump value:
$query = $wpdb->get_results("SELECT COUNT(*) FROM `$table` WHERE my mysql conditions");
var_dump($query); is this:
array(1) { [0]=> object(stdClass)#414 (1) { ["COUNT(*)"]=> string(3) "494" } }
How can I get the number 494 inside a variable?
$number = $query[0];
It'd be cleaner if you aliased your count so:
SELECT COUNT(*) AS `count`
then
$number = $query[0]->count;
If you only need to retrieve a single value, use $wpdb->get_var() instead of get_results(). This method exists specifically for situations like yours.
$number = $wpdb->get_var("SELECT COUNT(*) FROM `$table` WHERE my mysql conditions");

Zend mysql fetchAll and count

I have following SQL query:
SELECT `ka`.`id`, COUNT(kk.id) AS `clicks` FROM `karriere_anzeige` AS `ka` LEFT JOIN `karriere_klicks` AS `kk` ON `ka`.`id` = `kk`.`id_anzeige` WHERE (ka.id_kunde = '616') GROUP BY `ka`.`id`
If I run this query in phpMyAdmin I get proper results, f.e.:
id | clicks
4803 | 75
4822 | 144
However this attempt:
$rowset = $db->fetchAll($select);
Returns this rowset:
array(2) {
[0] => array(2) {
["id"] => string(4) "4803"
["clicks"] => string(1) "0"
}
[1] => array(2) {
["id"] => string(4) "4822"
["clicks"] => string(1) "0"
}
}
$db is Zend_Db_Adapter_Pdo_Mysql object
When I do INNER JOIN instead of LEFT JOIN, running the SQL query in phpMyAdmin returns few rows. Doing the same with Zend Framework, as described above, returns zero rows. I suppose I do generally something wrong, but I can't figure out what is it. Can somebody give me a hint?
if you are trying to pass the string as SQL try somethng like:
//This assumes access from a controller/action
$db = Zend_Db_Table::getDefaultAdapter();
$sql = "SELECT `ka`.`id`, COUNT(kk.id) AS `clicks` FROM `karriere_anzeige` AS `ka` LEFT JOIN `karriere_klicks` AS `kk` ON `ka`.`id` = `kk`.`id_anzeige` WHERE (ka.id_kunde = '616') GROUP BY `ka`.`id`";
$stmt = new Zend_Db_Statement_PDO($db, $sql);
$stmt->execute();
you can also build a Zend_Db_Select object to query your table with.

Categories