Error fetching data from database and undefine index - pdo

I'm passing value 13 to processpayment.php, but the function i call does not return any value to $req_que_info.
<a href="processpayment.php?id=13"</a>
processpayment.php?id=13:
include("database.php");
$queue_id=$_GET['id'];
$req_que_info = $database->getQueInfo($queue_id);
$count = count($req_que_info);
echo $req_que_info['queue_id'];
echo $count;
database.php:
function getQueInfo($queue_id){
$q = "SELECT * FROM ".TBL_QUEPAY." WHERE queue_id = ?";
$stmt = $this->connection->prepare($q);
$stmt->execute(array($queue_id));
$dbarray = $stmt->fetchAll();
return $dbarray;
}

because fetchAll returns a group of arrays like
Array
(
[0] => Array
(
[queue_id] => 13
[name] => foo
[age] => 99
)
[1] => Array
(
[queue_id] => 13
[name] => bar
[age] => 88
)
)
and fetch an (single) array like
Array
(
[queue_id] => 13
[name] => foo
[age] => 99
)
try
echo $req_que_info[0]['queue_id'];
may you use
foreach($req_que_info as $info)
echo $info . "<br />";
just see whats in the array.
print_r($req_que_info);

Related

Notice: Undefined property: stdClass::$images in

i have a problem with a stdObject. i did not find a solution for my problem. there are products that have images
[products] => array
(
[0] => stdClass Object
(
[images] => Array
(
[0] => stdClass Object
(
[original_url] => /path/to/the/picture.jpg
But there are also products that have no images and [original_url] => /path/to/the/picture.jpg does not exist. How can I check if [original_url] => /path/to/the/picture.jpg does not exist?
foreach($product->images as $images){
$PIC_url = $images->original_url;
if(!property_exists($images,'original_url')){
$whole_pic_url = 'https://path.to/alternative/picture.jpg';
} else {
$whole_pic_url = "https://www.path.to".$PIC_url . ',';
}
}
best regards dashmir
i found the solution for my question -I put this code before the foreach loop.
if(property_exists($product, "images")){
foreach($product->images as $images){
$PIC_url = $images->original_url;
$whole_pic_url = "https://www.path.to".$PIC_url . ',';
echo "<pre>";
var_dump ($whole_pic_url);
echo "</pre>";
}
} else {
$whole_pic_url = 'https://path.to/alternative/picture.jpg';
}
FYI
Best regards
Dashmir

Codeigniter: Update multiple Rows in Codeigniter in one statement

I am having dictionary as
array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
)
While I have only 2 columns in my table, ID and Status. I want to update two rows having IDs 1, and 2, using only one time ($this->db->update). Is it possible or should I have to make custom query.
I want to do like ($this->db->update(where ID1 == 1 && ID2 == 2)).
Either
// Get your status
$item = array( 'Status' => $response['Status'] );
// unset status
unset($response['Status']);
// I assume except status rest all values are ids so
// array_values gives id
$this->db->where_in('id', array_values($response));
// Update table
$this->db->update('table', $item );
OR
You have to modify your array
$data = array(
array(
'id' => 1 ,
'Status' => 'Done'
),
array(
'id' => 2 ,
'Status' => 'Done'
)
);
and then
$this->db->update_batch('table_name', $data, 'id');
Like this you can modify
( for comment : Dear I cant do so, because I am getting response from some service. And I am not able to edit this response. )
[akshay#localhost tmp]$ cat test.php
<?php
$response = array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
);
$data = array();
$item = array( 'Status' => $response['Status'] );
unset($response['Status']);
foreach($response as $new){
$item['id'] = $new;
$data[] = $item;
}
print_r($data);
// Here you update
// $this->db->update_batch('table_name', $data, 'id');
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => Array
(
[Status] => Done
[id] => 1
)
[1] => Array
(
[Status] => Done
[id] => 2
)
)
//this will update the values where ids equal any value in the second were_in parameter
//data is array of values to update
$data = [
'Status'=>'some status',
];
//array of ids to update
$ids = [1,2];
$this->db->where_in('id', $ids)->update('table_name', $data);

Eloquent select from array in the same order

I have this eloquent call:
$array = [100, 200, 50, 3, 300];
$response = EloquentModel::whereIn('id', $array)->get();
but now I want the results to be order in the same order in which the array is ordered; so the result should be like:
0 => Obj(
[id] => 100
[name] => name100
)
1 => Obj(
[id] => 200
[name] => name200
)
2 => Obj(
[id] => 50
[name] => name50
)
3 => Obj(
[id] => 3
[name] => name3
)
....
The order is specified by how the $array variable has it's elements ordered...
Is it possible to do something like that in Eloquent?
Ok, so the solution is as follow:
$response = EloquentModel::whereIn('id', $array)->get()->sortBy(function($item, $index) use($array){
$arrayToSortBy = array_flip($array);
return $arrayToSortBy[$item->id];
});
Basically we can sort Eloquent:Collections response.
array_flip will flip keys with values, so that we can return the relevant number for ordering....

how to execute stored procedure in cakephp 3?

This is my function for login i call a stored procedure login for match user name and password, but it doesn't work for me it give me all the rows:
public function login($email,$password)
{
$consumers = TableRegistry::get('Consumers');
$result=$consumers->query("Call login('".$email."','".$password."')");
pr($result->toArray());die;
}
My stored procedure in phpmyadmin is below:
BEGIN
SELECT * FROM consumers WHERE email = email_id AND password = md_password;
END
When i am executing query it give me object but after converting this object into array it give me all the row of table.output is:
<pre class="pr">Array
(
[0] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 1
[name] => jeevan
[email] => j#gmail.com
[password] => asdf
[phone_no] => 8447726137
[ota] => cde
[status] => 0
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-08 17:28:52
[timezone_type] => 3
[timezone] => UTC
)
[token_access] =>
[device_type] => 1
[push_id] => abc
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
[1] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 2
[name] => jack
[email] => jack#gmail.com
[password] => 123
[phone_no] => 7409757656
[ota] => chb
[status] => 1
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-20 06:10:14
[timezone_type] => 3
[timezone] => UTC
)
[token_access] => ghcvhgv
[device_type] => 0
[push_id] => hgnjh
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
)</pre>
that mean the stored procedure not working, any idea really help me, thanks in advance!
You can call a stored procedure using the ConnectionManager's execute() method. It's important to remember to prepare the query so that you remove the risk of SQL injection:-
$this->connection = ConnectionManager::get('default');
$results = $this->connection->execute(
'CALL login(?, ?)',
[$email, md5($password)]
)->fetchAll('assoc');
When CakePHP runs execute() it will substitute the ? with the escaped and quoted values of $email and md5($password). For further details check out the official docs on Preparing a statement.
step 1:use the class as use Cake\Datasource\ConnectionManager;
step 2: then execute the procedure-
$this->connection = ConnectionManager::get('default');
$consumer = $this->connection->execute("CALL login('".$email."','".md5($password)."')")->fetchAll('assoc');
now it will give an array.
finally i got this answer in cakephp cookbook.

Saving a Multidimensional to Database

Array
(
[name] => Array
(
[0] => 1
[1] => 1
)
[age] => Array
(
[0] => today
[1] => today
)
[grp] => Array
(
[0] => 2
[1] => 2
)
)
How I can save this into a table in yii. name, age and grp can have unlimited number of values.
table id, name, age and grp
if each array index is new record in your db , you can use this in your controller:
$lenth = count($yourArray['name']); // this line will return the count of records must be inserted to db table
for( $i = 0 ; $i < $lenth ; $i++ )
{
$newRecord = new ModelName;
$newRecord->name = $yourArray['name'][$i];
$newRecord->age = $yourArray['age'][$i];
$newRecord->grp = $yourArray['grp'][$i];
$newRecord->save();
}