I want to convert this query into medoo:
$update_data = "UPDATE users SET `featured` = CASE WHEN `start_date` = '2022-08-10' AND `stop_date` = '2022-08-14' THEN '1' ELSE '0' END
";
I've tried to convert it but I'm getting an error:
tried this:
$update_data = $db->update("users", `featured`, [
'AND'=>[ `start_date` => '2022-08-10', `stop_date` => '2022-08-14'] ? [`featured` => '1'] : [`featured` => '0']
]);
Related
My Query return always the Mysql HY093 error but why?
I have all parameters and still its not working...
Can someone help ?
// create PDO params array
$params = [
':employee ' => $formData['employee'],
':usedFrom' => $formData['usedFrom'],
':usedTill' => $formData['usedTill'],
':company' => $formData['company'],
':costCenter' => $formData['costCenter'],
':vehicle' => $formData['vehicle'],
':vehicleTag' => $formData['vehicleTag'],
':updatedAt' => $formData['updatedAt'],
':ID' => $formData['ID']
];
// update entry
$return = $db->pquery(
'UPDATE entry SET employee_id = :employee, used_from = :usedFrom, used_till = :usedTill, company_id = :company, cost_center_id = :costCenter, vehicle_id = :vehicle, vehicle_tag = :vehicleTag, updated_at = :updatedAt WHERE ID = :ID',
$params
);
For some weird reason, my PDO keeps returning NULL when it should return a string/integer. I've tried to use the odbc_conect and it works fine.
Context:
CentOS 7
PHP 7.1.13
unixODBC 2.3.1 (x86_64 . 11.el7)
ODCB Driver: SAP HANA ODBC v2.2.36.1512099132 64 bits
When it's happening? Until now, I detected few:
Function returns: SELECT id,GEO.ST_Asgeojson() FROM DEV.GEOMETRIES
Using CAST: SELECT id, CAST(GEO.ST_AsText() AS VARCHAR) FROM DEV.GEOMETRIES or SELECT CAST(1 AS INTEGER) AS "int" FROM DUMMY
Any blob column : SELECT id,GEO FROM DEV.GEOMETRIES
Date Types: SELECT insertdate FROM DEV.GEOMETRIES
I also tried to use PDOStatement::bindColumn to force data types but was unsuccessful
Where the code :
<?php
try{
ini_set('memory_limit','256M'); //odbc_fetch_array is hungry
// $sql = 'SELECT NOW() AS "now" FROM DUMMY';
$sql = "SELECT id,GEO.ST_Asgeojson(),GEO.ST_AsText(),insertdate FROM DEV.geometries limit 1";
//$sql = "SELECT * FROM DEV.geometries limit 1";
// $sql = "SELECT CAST(GEO.ST_AsText() AS VARCHAR) from geometries";
// $sql = "SELECT CAST(10 AS VARCHAR) from DUMMY";
// $sql = "SELECT CAST(10 AS INTEGER) from DUMMY";
echo "SQL: ",$sql,PHP_EOL;
$DSN = "DRIVER=HANADB;UID=DEV;PWD=DEV#pwd;SERVERNODE=192.168.1.163:39013;DATABASENAME=SYSTEMDB";
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_CASE =>\PDO::CASE_LOWER, //Compatibility with mysql case
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES, true //With or without
];
echo "############# Using PDO:ODBC:",PHP_EOL;
$DB = new \PDO("odbc:".$DSN,'DEV','DEV#pwd',$options);
echo "Driver: ",$DB->getAttribute(\PDO::ATTR_CLIENT_VERSION),PHP_EOL;
echo "Using Prepared Statement:",PHP_EOL;
$stmt = $DB->prepare($sql);
// bindColumn Also doesn't work
//$stmt->bindColumn('id', $id, \PDO::PARAM_INT);
//$stmt->bindColumn('geojson', $geometry, \PDO::PARAM_STR);
$stmt->execute();
/*while ($row = $stmt->fetch(\PDO::FETCH_BOUND)) {
var_dump($row);
var_dump($id, $geometry);
}*/
//var_dump($ret,$stmt);
var_export($stmt->fetchAll());
$stmt->closeCursor();
echo PHP_EOL,"Using Query:",PHP_EOL;
$qR = $DB->query($sql);
foreach($qR as $row){
var_export($row);
}
echo PHP_EOL,PHP_EOL,"############# Using odbc_*( ) Functions",PHP_EOL;
$conn = odbc_connect($DSN,'DEV','DEV#pwd');
$rs = odbc_exec($conn,$sql);
while($row = odbc_fetch_array($rs)){
var_export($row);
}
}catch(\Exception $e){
echo $e->getMessage(),PHP_EOL,$e->getTraceAsString();
}
Output:
############# Using PDO:ODBC:
Using Prepared Statement:
array (
0 =>
array (
'id' => '363491',
'geojson' => NULL,
'geotext' => NULL,
'__dateinsert' => NULL,
'info' => NULL,
),
)
Using Query:
array (
'id' => '363491',
'geojson' => NULL,
'geotext' => NULL,
'insertdate' => NULL,
'info' => NULL,
)
############# Using odbc_*( ) Functions
array (
'id' => '363491',
'geojson' => '{"type": "Point", "coordinates": [16.352878, 48.225628]}',
'geotext' => 'POINT (16.352878 48.225628)',
'insertdate ' => '2018-02-06 15:08:19.414000000',
'info' => NULL,
)
I'm not sure if it's a Drive problem because it works with odbc_* functions, maybe it's a PDO BUG.
This is my query:
SELECT * FROM mytable WHERE `USER` = '1599' AND `id` = '124253' AND `wednesday` IS NULL
This is the php code:
$check = Test::model()->find("id =:id and user =:user and ".$day." =:day ", array(":id" => $check_lunch_per_day, ":user" => $user, 'day'=> null));
another try:
$check = Test::model()->find("id =:id and user =:user and ".$day." =:day ", array(":id" => $check_lunch_per_day, ":user" => $user, 'day'=> 'IS NULL'));
But the result of the check is always null. What should I do? I want to get the wednesday column value if it is null.
This should work,
setting :day to IS NULL
$check = Test::model()->find("id =:id and user =:user and ".$day." =:day ",
array(":id" => $check_lunch_per_day, ":user" => $user, ':day'=> 'IS NULL'));
How can I dot that in CakePHP?
I try everything I know, but doesn't work:
select
card_type
,sum (case when used = 'Y' then (select amount from auth a1 where a1.origid = a.pnref and trxtype = 'D') else amount end) as total
from auth a
where
add_date between '$this->date1' and '$this->date2'
and (trxtype = 'S' or trxtype = 'F')
and user_num = $this->user_num
and pnref not in (select origid
from auth a
where add_date between '$this->date1' and '$this->date2'
and trxtype = 'V')
group by card_type
order by 1
Sub-query in fields
Sub-query in where
[I try this]:
$conditionsSubQuery['"Auth2"."add_date BETWEEN ? AND ?"'] = array($inicial_date, $final_date);
$conditionsSubQuery['"Auth2"."trxtype"'] = 'V';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"Auth2"."origid"'),
'table' => 'auth',
'alias' => 'Auth2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->Auth
);
$subQuery = ' "Auth"."pnref" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$conditions[] = array(
'Auth.date BETWEEN ? and ?' => array($inicial_date, $final_date),
'OR' => array(
'Auth.trxtype' => 'S',
'Auth.trxtype' => 'F'
),
'Auth.user_num' => $user_num
);
$fields = array(
'Auth.card_type',
"sum (case when Auth.used = 'Y' then (select Auth2.amount from auth Auth2 where Auth2.origid = Auth.pnref and Auth.trxtype = 'D') else Auth.amount end) as Auth.total"
);
$group = array('Auth.card_type');
$order = array(1);
return $this->Auth->find('all', compact('fields', 'conditions', 'group', 'order'));
[ERROR]
Database Error
Error: SQLSTATE[42601]: Syntax error: 7 ERRO: erro de sintaxe em ou próximo a "." LINE 1: ... Auth.trxtype = 'D') else Auth.amount end) as Auth.total, "U... ^
SQL Query: SELECT "Auth"."card_type" AS "Auth__card_type", sum (case when Auth.used = 'Y' then (select Auth2.amount from auth Auth2 where Auth2.origid = Auth.pnref and Auth.trxtype = 'D') else Auth.amount end) as Auth.total, "User"."user_num" AS "User__user_num" FROM "public"."system_users" AS "User" WHERE "Auth"."pnref" NOT IN (SELECT "Auth2"."origid" FROM auth AS "Auth2" WHERE "Auth2"."add_date BETWEEN '2013/02/19 06:00:00' AND '2013/02/22 10:34:17'" AND "Auth2"."trxtype" = 'V' ) AND (("Auth"."date" BETWEEN '2013/02/19 06:00:00' and '2013/02/22 10:34:17') AND ("Auth"."trxtype" = 'F') AND ("Auth"."user_num" = 68)) GROUP BY "Auth"."card_type" ORDER BY "1" ASC
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
Anyway, thanks.
Like #Chris Traveres comment, is just a syntax error.
The problem in the query is pretty obvious that the identifier is
being incorrectly associated. the relevant clause of the error
message's query is "Auth2"."add_date BETWEEN '2013/02/19 06:00:00' AND
'2013/02/22 10:34:17'" and I doubt you have a boolean column named
"add_date BETWEEN '2013/02/19 06:00:00' AND '2013/02/22 10:34:17'" so
the question really should be limited to CakePHP syntax
Problem solved!
Thanks guys.
How do I pass mysql's CURRENT_TIMESTAMP when using Zend_DB's update statement? The following doesnt seem to be working.
I have something like this:
$update = array(
'Name' => 'John',
'DT_Modified' => 'CURRENT_TIMESTAMP'
);
$db->update('usertable', $update );
to run a query that is represented like this:
UPDATE usertable SET Name='John', DT_Modified = CURRENT_TIMESTAMP
Try using Zend_Db_Expr to avoid unnecessary quoting:
$update = array(
'Name' => 'John',
'DT_Modified' => new Zend_Db_Expr('CURRENT_TIMESTAMP')
);
$db->update('usertable', $update );