Optimize Query on Models - php

I have a problem with displaying a bunch of data from my database. Getting data from many tables.
My query before was getting all the data in one query then I had managed to find a way on how to optimize it but I'm not satisfied.
foreach($query->result_array() as $row ){
$drreturn = $this->getDRReturnAmount($row["idno"],$date1);
$return_amount[] = $drreturn->return_amount;
$checkDRServiceNoPayment = $this->checkDRServiceNoPayment($row["idno"],$date1,$date2);
if($checkDRServiceNoPayment->num_rows() > 0) {
$minus_item_amount[] = 0;
$getProductPurchase = $this->getProductPurchase($row["idno"],$date1,$date2);
$minus_item_amount[] = $getProductPurchase->amount;
}
}
btw I'm using datatable. what I did before was I didn't use an array and I put the model functions inside the foreach but it seems that nothing happened even tho I used array.

Related

With php ibase is there any way of returning to the first row after reading several rows (to avoid re-querying the database)

With php ibase, ibase_query fetches the relation, several functions can then fetch rows from that relation, but all these functions extract the next row.
In Delphi I have the ability to return to the first row (DataSet.First;) - is there any equivalent in PHP ibase?
I could of course re-query the database, but this seems a waste of resources if the original relation is still accessible.
Example code:
$table = ibase_query($sql);
$row = ibase_fetch_object($table);
while (!empty($row))
{
echo $row->ENTRYNO.'<br>';
$row = ibase_fetch_object($table);
}
//The missing functionality
$table.First;
//or maybe
$row = ibase_fetch_object($table,first);
//in which case of course the following line would be redundant
$row = ibase_fetch_object($table);
while (!empty($row))
{
//process record
$row = ibase_fetch_object($table);
}
The ibase/firebird-php driver is only able to fetch forward. If you want to consult earlier rows, you will need to store them yourself in some form, or execute the query again.

Cannot update all columns using looping

i have some data to update, and I want to update all the rows in my table using that data. So I tried to get all my data, then loop through my data and set the rows using new data, but it only works for the first row. How do I update all my rows?
this is my code, i using activerecord to update:
$replaceid = $_POST['pid'];
$product = ProductModel::find()->where(['createdby'=>$uid])->orWhere(['modifiedby'=>$uid])->all();
if(isset($product)) {
foreach ($product as $p) {
$p->createdby = $replaceid;
$p->modifiedby = $replaceid;
$p->update(false);
}
}
this is only update createdby but for modifiedby still same.. what's wrong here?
This can be done completely on the database-side, therefore negating any issues you might have in PHP
ProductModel::query()->where(['createdby'=>$uid])->orWhere(['modifiedby'=>$uid])->update(['createdby' => $replaceid, 'modifiedby'=> $replaceid]);

Querying multiple mysql tables with php variables

I have a list of table names in my $_POST variable and I am trying to feed them into a MySQL query. My ultimate goal is to be able to select what tables I want to pull data from one page and then be able to use that data. Everything is accurate and working in my $_POST variable, I just don't know how to pass that onto the query string.
Using the code below, I'm not getting any results back at all. I'm fairly new and completely pulling my hair out. Any help would be greatly appreciated, even if it is just a point in the right direction.
foreach ($_POST as $item) {
if ($item != 'Submit') {
$query = "SELECT * FROM " . $item;
$result = $database->query($query);
$$item = mysqli_fetch_array($result);
}
}

Codeigniter Database Record retrival - Best Practise - Performance

I'm checking an PHP project which is build using PHP Codeigniter. I'm new to PHP, I like to get your valuable feedback
To retrieve name and item number in php library/controller, call is made to Item Model as below
'name'=>$this->CI->Item->get_info($item_id)->name
'item_number'=>$this->CI->Item->get_info($item_id)->item_number
I suspect above two line of code will make two independent database sql call instead of one call to retrive the two column of same table. Ie., there will be performance degrade. But somebody can please let me know whether it fires two sql statements please?
I think we need to handle like object
$row = $this->CI->Item->get_info($item_id);
echo $row->name;
echo $row->item_number;
Please suggest. Thanks in advance.
Model Function:
function get_info($item_id)
{
$this->db->from('items');
$this->db->where('item_id',$item_id);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$item_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('items');
foreach ($fields as $field)
{
$item_obj->$field='';
}
return $item_obj;
}
}

How would I do this in php?

I'm implementing a memcache to cache mysql query results.
It loops through the mysql results using
while($rowP3= mysql_fetch_array($result3)) {
or it loops through the memcached results (if they are saved in memcache, and not expired) via
foreach($cch_active_users_final as $rowP3) {
How can I get it to show the right looping method based on if the memcached value exists or not. I just want it to pick the right looping method. I could just duplicate the entire while { } function with all its contents, but I don't want to repeat that huge chunk of code, just so I can change while to foreach
The logic should be something like:
(partial pseudocode)
$data = get_memcached_data('cch_active_users_final');
if (!$data) {
$query = mysql_query(..);
$data = array();
while($row = mysql_fetch_array()) {
$data[] = $row;
}
store_in_memcache('cch_active_users_final', $data);
}
// do whatever you want with $data
If I understand your question, this code doesn't make sense. After querying database comparing queried results to cached values doesn't make sense.
If the $row and $rowP3 values contain the same data, you could have whatever happends in the loops in a function, and pass the row as an argument.
You don't, you use a unified method using PDO.

Categories