PDO and prepared statements - pdo

I'm trying to implement this query:
SELECT AVG(timediff(end, start)) AS avg_in FROM phone_call
WHERE user_id=2
AND direction = 'o'
AND DATE(start) = DATE('2013-11-04 17:11:37')
using PDO with PHP. Could you provide some advice on how to do this. Ive tried:
$uid = 2; $direction='o'; $date='2013-11-04 17:11:37';
$q = "SELECT AVG(timediff(end, start)) AS avg_in FROM phone_call
WHERE user_id = ?
AND direction = ?
AND DATE(start) = DATE(?))";
$stmt = $this->pdo->prepare($q);
$stmt->bindParam(1, $uid, PDO::PARAM_INT, 5);
$stmt->bindParam(2, $direction, PDO::PARAM_STR, 1);
$stmt->bindParam(3, $date, PDO::PARAM_STR, 20);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//$result = round($result,2);
return $result;
but this fails.
edit:
client code:
$a = $call_log->getAvCallIn(2,'o','2013-11-04 17:11:37'); //
print '<pre>';print_r($a);
nothing get printed to the screen!
I suspect it's the
AND DATE(start) = DATE(?))
part of the query that is faulty?

Related

Query with doctrine 1

I try to make the following sql query from phpMyAdmin who works perfectly and return 1 result with doctrine 1 but i get an exception :
SQLSTATE[42S22]: Column not found: 1054 Champ 'MOY1100' inconnu dans
where clause. Failing Query: "select id_au FROM acteur_unite WHERE
code_unite = MOY1100 LIMIT 1"
Here the sql query who work on phpMyAdmin :
SELECT id_au FROM acteur_unite WHERE code_unite = 'MOY1100' LIMIT 1
Here my query with doctrine :
public function getId($code_unite) {
$con = Doctrine_Manager::getInstance()->connection();
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = $code_unite LIMIT 1");
$id = null;
// fetch query result
$data = $st->fetch(PDO::FETCH_ASSOC);
$id = $data['id_au'];
return $id;
}
Where i'm wrong ?
Thanks a lot in advance
seems you missing the quote around var $code_unite
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = '$code_unite' LIMIT 1");
but be careful with the use of var in sql .. you are at risk for sql injection . Then check for your framework the right way for the param_binding .. for avoid this risk
eg:
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = :code_unite LIMIT 1");
$st->bindParam(':code_unite', $code_unite, PDO::PARAM_STR);

Equal in select does not work as expected

I found a funny behaviour using equal with pdo. I checked several select-statements in phpmyadmin and pdo and the results vary. Has someone a idea whats wrong?
$sql = "select * from fenster where fenster > 'news'"; //works ok
$sql = "select * from fenster where fenster < 'news'"; //works ok
$sql = "select * from fenster where fenster <= 'news'"; //works ok
$sql = "select * from fenster where fenster >= 'news'"; // news is not in result
$sql = "select * from fenster where fenster = 'news'"; // news is not in result
$stmt = $db->prepare($sql);
$stmt->execute();
$feldName = array_keys($stmt->fetch(PDO::FETCH_ASSOC));
while($data = $stmt->fetch()) {
var_dump($data);
}
I found the reason myself.
The $feldName = array_keys($stmt->fetch(PDO::FETCH_ASSOC)); did read the missing row, so it did not show afterwards. So I have to find a workaround.

SELECT issue moving from PG_query to PDO

I have a select statement see below. Using PDO how would I recreate this same Select statement, as I want to grab two values from it and combine them into the $geomstring. I can figure out the combine, but not the first 3 lines.
$sql1 = "SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id='$_POST[gridref_id]'";
$result1 = pg_query($sql1);
$row1 = pg_fetch_array($result1);
$geomstring = $row1['easting_value']. $_POST['grid_eastings']." ".$row1['northing_value'].$_POST['grid_northings'];
*php website for prepared statements says *
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
I have something similar working for populating a dropdown that partly uses this
$stmt = $conn->prepare("SELECT easting_value, northing_value FROM gridreference_tbl WHERE gridref_id=$gridref_id");
$stmt->setFetchMode(PDO::FETCH_OBJ);
Found it on php.net, I was googling the wrong stuff:
$stmt4 = $conn->prepare("SELECT easting_value, northing_value from gridreference_tbl WHERE gridref_id = 4");
$stmt4->execute();
print("PDO::FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $stmt4->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");

how to get the second batch and 3rd batch in the same query result in oracle sql + yii framework?

let' say i have 20 results in the sql query. if am gonna use the limit in the yii active record, I'll obviously get the first four from the result, but what if i wanna get the 2nd four and then 3rd four in the same query result ? how to query that via sql ?
e.g
$criteria2 = new CDbCriteria();
$criteria2->select = 'USERID, ADID ,ADTYPE, ADTITLE, ADDESC, PAGEVIEW, DISPPUBLISHDATE';
$criteria2->addCondition("STATUS = 1");
$criteria2->order = '"t".PAGEVIEW DESC,"t".PUBLISHDATE DESC';
$criteria2->limit = 4;
$criteria2->with = array('subcat','adimages');
$result = $this->findAll($criteria2);
return $result;
Sorry :)
See here, how to paginate with Oracle (Are you use 11g?)
Alternatives to LIMIT and OFFSET for paging in Oracle
Well and in Yii just set offset, OCIConnector will set rownum automaticly for sql
$criteria2 = new CDbCriteria();
$criteria2->select = 'USERID, ADID ,ADTYPE, ADTITLE, ADDESC, PAGEVIEW, DISPPUBLISHDATE';
$criteria2->addCondition("STATUS = 1");
$criteria2->order = '"t".PAGEVIEW DESC,"t".PUBLISHDATE DESC';
$criteria2->limit = 4;
$criteria2->offset = 0; //4, 8 - COciCommandBuiled applyLimit use it
$criteria2->with = array('subcat','adimages');
$result = $this->findAll($criteria2);
return $result;

PDO SQL - Update query issue

I am new to pdo and do not get why the following insert query does not work. If I remove the line that executes the query, there will be of course no insertion, but there will be no error. If I leave that line, the script is not executed. Of course I checked and rechecked the table name and field name. Hope someone can hep me understand. Note that before executing the query, the ber_mBacth_date field of my table is set to NULL. Cheers. Marc
<?php
$db_host = 'localhost';
$db_user = 'user';
$db_password = 'user';
$db_database = 'myconsole';
$mBatchDate = date('Y-m-d H:i:s');
$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"');
$connexion = NULL;
?>
Can you try instead of:
$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"');
do:
$statement = $connexion->prepare("UPDATE batcherrors SET ber_mBatch_date = :mBatchDate");
$statement->bindValue(':mBatchDate', $mBatchDate, PDO::PARAM_STR);
$statement->execute();
Binding is recommended way to set parameters values (over concatenation).