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);
Related
When I use full PDO query:
$db = DB::singleton();
$stmt = $db->prepare("SELECT * FROM promocode WHERE status = 1 AND code='xyz'");
$stmt->execute();
PhpStorm recognize table scheme and it suggests syntax & show wrong column name - Nice!
...but, most often I use db like this:
public function get($coupon)
{
/* table:promocode */
$db = $this->db()->retObj();
$db->where('status = 1 AND code = ?', [$coupon]);
return $db->fetch();
}
Is there any "magic" comment like (table:promocode) to tell PhpStorm that it is DB syntax?
I'm trying to perform a search in one of my tables based on a given criteria like so:
$id = 1;
$criteria = new CDbCriteria();
$criteria->addCondition("usr_currency=:currency");
$currencies = User::model()->findAll($criteria, array(':currency' => $id,));
I get a CDbException:
CDbCommand failed to execute the SQL statement:
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound.
The SQL statement executed was:
SELECT * FROM `user` `t`
WHERE usr_currency=:currency
Where as, this works:
$id = 1;
$criteria = new CDbCriteria();
$criteria->addCondition("usr_currency=:currency");
$criteria->params = array(':currency' => $id,);
$comments = User::model()->findAll($criteria);
What is wrong with the first code fragment?
From CActiveRecord::find()
This is only used when the first parameter is a string (query condition). In other cases, please use CDbCriteria::params to set parameters.
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");
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;
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).