Foreaching PDO bindValue / BindParam - php

I wanted to make my life a bit easier like in this post:
PDO::bindParam in a foreach loop, all values are being set as the same?
And several others...
I tried several variants to get my code work (to see messages) but no result i have to methods in my class:
public function getMessages($locationId, $name) {
if(!empty($name)) {
$query = 'SELECT * FROM (SELECT * FROM sms WHERE location_id=:locationId ORDER BY id DESC LIMIT 5) AS SOURCE ORDER BY id ASC';
$parameters = array(':locationId' => $locationId);
$row = $this->returnDataObject($query, $parameters);
while ($row) {
$this->fetchData($row, $biggestId, $name);
}
$this->updateSessionKey($locationId, $name);
}
}
And the other:
public function returnDataObject($query, $parameters) {
var_dump($query);
var_dump($parameters);
$dataObject = $this->dbh->prepare($query);
foreach ($parameters as $key => &$value) {
$dataObject->bindParam($key, $value);
}
$dataObject->execute();
$row = $dataObject->fetch(PDO::FETCH_OBJ);
return $row;
}
I tried:
foreach ($parameters as $key => &$value) {
and:
foreach ($parameters as $key => $value) {
I tried:
$dataObject->bindParam($key, $value);
and:
$dataObject->bindValue($key, $value);
I also tried in getMessages method to die('I am in getMessages method');
But i couldn't get that far...
What should i do differently to get this code work?
I appreciate any help!
EDIT:
My bad with biggestId but declaring biggestId:
$biggestId = $this->getBiggestId($locationId);
But fixing that didn't fix the whole problem. This first method works perfictly if it is:
public function getMessages($locationId, $name) {
if(!empty($name)) {
$biggestId = $this->getBiggestId($locationId);
$messages = $this->dbh->prepare('SELECT * FROM (SELECT * FROM sms WHERE location_id=:locationId ORDER BY id DESC LIMIT 5) AS SOURCE ORDER BY id ASC');
$messages->bindParam(':locationId', $locationId);
$messages->execute();
while ($row = $messages->fetch(PDO::FETCH_OBJ)) {
$this->fetchData($row, $biggestId, $name);
}
$_SESSION[$name] = $biggestId;
}
}
But i want to use the method so i don't have to bindParam / BindValue manualy, the functionality of returnDataObject method would make my life easier...

Make the following Changes:
use fetchAll() instead of fetch()
Since fetchAll() is used, use a foreach rather than a while loop.
returnDataObject:
public function returnDataObject($query, $parameters) {
$dataObject = $this->dbh->prepare($query);
foreach ($parameters as $key => &$value) {
$dataObject->bindParam($key, $value);
}
$dataObject->execute();
$rows = $dataObject->fetchAll(PDO::FETCH_OBJ);
return $rows;
}
getMessages:
public function getMessages($locationId, $name) {
if(!empty($name)) {
$query = 'SELECT * FROM (SELECT * FROM sms WHERE location_id=:locationId ORDER BY id DESC LIMIT 5) AS SOURCE ORDER BY id ASC';
$parameters = array(':locationId' => $locationId);
$rows = $this->returnDataObject($query, $parameters);
foreach($rows as $row) {
$this->fetchData($row, $biggestId, $name);
}
$this->updateSessionKey($locationId, $name);
}
}

Related

Returns multiple values from a PHP function

Apologize for the repeated question. Return multiple values from database with function. I tried executing code, the function returns one value, where I want all the values of id and name.
Database: id and name has 9 rows. Is there anything I was missing in my code.
function readdata() {
$sth = $db->execute('SELECT * FROM mynumbers m WHERE m.id>1 ORDER BY m.id ASC');
foreach ($sth as $s) {
$object = new stdClass();
$object->id = $s->id;
$object->name = $s->name;
return $object;
}
}
$rd = readdata();
echo $rd->id;
echo $rd->name;
May be something like this:
function readdata() {
$sth = $db->execute('SELECT * FROM mynumbers m WHERE m.id>1 ORDER BY m.id ASC');
$out = [];
foreach ($sth as $s) {
$object = new stdClass();
$object->id = $s->id;
$object->name = $s->name;
$out[] = $object;
}
return $out;
}
$rd = readdata();
//and here
foreach($rd as $obj){
echo $obj->id;
echo $obj->name;
}
This is more a suggestion than an answer. Why re-inventing the wheel? PDO already is capable of returning classes and also fetching all results into an array.
function readdata(PDO $db): array
{
$sth = $db->prepare('SELECT * FROM mynumbers m WHERE m.id>1 ORDER BY m.id ASC');
$sth->execute();
return $sth->fetchAll(PDO::FETCH_CLASS);
}
$objects = readdata($db);
$objects is now an array. Each element contains a stdClass object with each column name as property.
foreach($objects as $object) {
echo $object->id, PHP_EOL;
echo $object->name, PHP_EOL;
}
Your foreach loop intends to run through all values of the array $sth, but returns only with the FIRST one.
You can just return $sth to get the whole array, or build a new array and append to it:
$ret = array();
foreach ($sth as $s) {
...
$ret[] = $object;
}
and then
return $ret;

MySQL PDO resultSet as object list performance

I'm using the following code to get records from DB:
$stmt->setFetchMode(PDO::FETCH_CLASS, MyClass::class);
$resultSet = $stmt->fetchAll();
return $resultSet;
This works fine.
However sometimes the PHP class property names and the database field names are different. In this case we make a mapping as follows:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$resultSet = $stmt->fetchAll();
foreach ($resultSet as $record) {
$resultList[] = $this->convertDbArrayToObject($record, MyClass::class, $mapping);
}
return $resultList;
where
public function convertDbArrayToObject($record, $class, $mapping = null)
{
$object = new $class();
foreach ($record as $key => $value) {
if (array_key_exists($key, $mapping)) {
$fieldName = $mapping[$key];
$setterName = 'set' . ucfirst($fieldName);
$object->$setterName($value);
}
}
return $object;
}
The problem is that the second solution is about ~10-20 times slower that is way too much.
Any idea how could we improve the performance?

N1QL prepared statement not working 100%

I have a problem where some variables are probably (I can't know for sure) not inserted into the final statement. Here is my example:
Works:
public static function findByPageAndFieldContains($recordsPerPage, $page, $field, $searchterm) {
$query = CouchbaseN1qlQuery::fromString('SELECT * FROM `public_portal` WHERE `collection`=$collection AND TOSTRING('.$field.') LIKE "%'.$searchterm.'%" ORDER BY `_id` limit $limit offset $offset');
$query->options['$collection'] = static::COLLECTION_NAME;
//$query->options['$field'] = $field;
$query->options['$limit'] = $recordsPerPage;
$query->options['$offset'] = $recordsPerPage*($page-1);
//$query->options['$searchterm'] = $searchterm;
$result = DB::getDB()->query($query);
var_dump($query);
var_dump($result);
$objects = array();
foreach($result as $row) {
$object = new static($row->{"public_portal"});
$object->setId($row->{"public_portal"}->{"_id"});
$objects[] = $object;
}
//var_dump($objects);
return $objects;
return $result;
}
Debug Output:
debug01
Does not work:
public static function findByPageAndFieldContains($recordsPerPage, $page, $field, $searchterm) {
$query = CouchbaseN1qlQuery::fromString('SELECT * FROM `public_portal` WHERE `collection`=$collection AND TOSTRING($field) LIKE "%$searchterm%" ORDER BY `_id` limit $limit offset $offset');
$query->options['$collection'] = static::COLLECTION_NAME;
$query->options['$field'] = $field;
$query->options['$limit'] = $recordsPerPage;
$query->options['$offset'] = $recordsPerPage*($page-1);
$query->options['$searchterm'] = $searchterm;
$result = DB::getDB()->query($query);
var_dump($query);
var_dump($result);
$objects = array();
foreach($result as $row) {
$object = new static($row->{"public_portal"});
$object->setId($row->{"public_portal"}->{"_id"});
$objects[] = $object;
}
//var_dump($objects);
return $objects;
return $result;
}
Debug output:
debug02
Basically the second example returns no result, while the first one works just fine.
Any idea why?
You are not using N1QL parameters correctly. You must decide if you are evaluating your parameters in PHP or in N1QL.
The field name cannot be a N1QL parameter, so you evaluate it in PHP:
TOSTRING('.$field.') LIKE ...
The search term should be a N1QL parameter, so you add wildcards in PHP and then pass it to N1QL as a parameter:
$searchterm = '%'.$searchterm.'%'
TOSTRING('.$field.') LIKE $searchterm ...

get array values with a function in PHP

What is the proper way to create a function to get array values from a query? I am getting "Undefined index: page_title" error.
However I can get the value without function like: echo $row->page_title;
Or echo $query[0]->title;
function get_page($id) {
$db = new DB();
$query = $db->get_rows("SELECT * FROM pages WHERE id = :id ", array('id' => $_GET['id']) );
foreach ($query as $row) {
$page_id = $row->id;
$page_title = $row->title;
}
return $query;
}
$page = get_page(1);
echo $page['page_title'];
here is my database class:
function get_rows($query, $values=array(), $fetchType = FETCH_OBJ)
{
$sth = $this->dbh->prepare($query);
if(is_array($values) && (sizeof($values) > 0))
{
foreach($values as $key=>$val)
{
$key = is_numeric($key) ? ($key + 1) : $key;
$sth->bindValue($key,$val);
}
}
if($sth->execute())
return $sth->fetchAll($fetchType);
}
To make the function reusable, I would rewrite it the following way
function get_page($id, $col) {
$db = new DB();
$query = $db->prepare('SELECT * FROM pages WHERE id = :id');
$query->execute(array(':id' => $id));
$results = $query->fetch(PDO::FETCH_NUM);
return $results[0][$col];
}
$page = get_page(1, 'page_title');
echo $page;
I skipped the foreach as you said that all id's are unique so you should only ever have 1 result
Also it may not be a bad idea to add some error checking to make sure you do get back what you expect from the query and to make sure it is not empty.
Edit: Sorry if the syntax is a little off, dont have anything to test the code against quickly.

PHP Reference for MySQL query

My function:
function sql_query($s, $x) {
$query = mysql_query($s);
global $mysql;
while($mysql = mysql_fetch_array($query)) {
return;
}
}
Now it's work only with $mysql variable:
echo $mysql['username'];
How to make it works only with:
sql_query("select * from users where id = '1' limit 1", "varname");
$varname['username'];
I want to set a SQL Query and Variable name in function, like:
sql_query("sqlquery", "variable");
echo $variable['id'];
Thanks for reply!
function sql_query($s, &$x) {
global $mysql;
$query = mysql_query($s);
$result = mysql_fetch_array($query);
foreach($result as $key => $value) {
$x[$key] = $value;
}
}
This should assign each variable that is returned by the query to a key in array $x (assuming it is an array). Notice that I am passing $x by reference instead of by value, eliminating the need to return anything.

Categories