PHP SQL PDO Memcached not storing results in cache - php

Looking for some advice.
Trying to return a long query with around 65,000 results that has a lot of aggeration on the server-side. Takes around 16 seconds to return all the data.
This is what I have so far.
It's returning the query results but never seems to store in Memcached.
If I print $cached_data, it always says false. Thank you.
<?php
header('Content-Type: application/json');
include('pdo.php');
$memcache = new Memcache();
$memcache->addServer("127.0.0.1", 11211);
$query = "SELECT TOP 5000 Snumber,Number,Name,Status,dials,Size,Y04,Y05,PD0405,Y06,PD0506,Y07,PD0607,Y08,PD0708,Y09,PD0809,Y10,PD0910,Y11,PD1011,Y12,PD1112,Y13,PD1213,Y14,PD1314,Y15,PD1415,Y16,PD1516,Y17,PD1617,Y18,PD1718,Y19,PD1819,Y20,PD1920 FROM table ORDER BY Snumber";
$key = md5($query);
$cached_data = $memcache->get($key);
$response = [];
if ($cached_data != null) {
$result = $cached_data;
} else {
$statement = $conn->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$memcache->set($key, $result);
}
foreach ($result as $row) {
$output[] = array(
'Snumber' => $row['Snumber'],
'MeterNumber' => $row['Number'],
'Account' => $row['Name'],
'Status' => $row['Status'],
'dialsHI' => $row['dials'],
'MeterSize' => $row['Size'],
'Y04' => $row['Y04'],
'Y05' => $row['Y05'],
'PD0405' => $row['PD0405'],
'Y06' => $row['Y06'],
'PD0506' => $row['PD0506'],
'Y07' => $row['Y07'],
'PD0607' => $row['PD0607'],
'Y08' => $row['Y08'],
'PD0708' => $row['PD0708'],
'Y09' => $row['Y09'],
'PD0809' => $row['PD0809'],
'Y10' => $row['Y10'],
'PD0910' => $row['PD0910'],
'Y11' => $row['Y11'],
'PD1011' => $row['PD1011'],
'Y12' => $row['Y12'],
'PD1112' => $row['PD1112'],
'Y13' => $row['Y13'],
'PD1213' => $row['PD1213'],
'Y14' => $row['Y14'],
'PD1314' => $row['PD1314'],
'Y15' => $row['Y15'],
'PD1415' => $row['PD1415'],
'Y16' => $row['Y16'],
'PD1516' => $row['PD1516'],
'Y17' => $row['Y17'],
'PD1617' => $row['PD1617'],
'Y18' => $row['Y18'],
'PD1718' => $row['PD1718'],
'Y19' => $row['Y19'],
'PD1819' => $row['PD1819'],
'Y20' => $row['Y20'],
'PD1920' => $row['PD1920']);
}
echo json_encode($output, JSON_PRETTY_PRINT);

Related

Pushing Into An Array MySQL Values

I am trying to push values from a sql query into an array using array_push.
Everything works fine except one field - "Beschreibung". It's a MySQL Text Field. When I put in the row "Beschreibung" the output from the PHP is completely empty.
Here is my code so far, any help is appreciated!
while ( $row = $result->fetch_assoc()) {
array_push($data, array(
"Beschreibung" => $row["Beschreibung"],
"id" => $row["id"],
"Titel_Veranstaltung" => $row["Titel_Veranstaltung"],
"Strasse" => $row['Strasse'],
"PLZ" => $row["PLZ"],
"Ort" => $row["Ort"],
"Bild" => $row["Bild"],
"Telefon" => $row["Telefon"],
"Datum" => $row["Datum"],
"Uhrzeit" => $row["Uhrzeit"],
"Latitude" => $row["latitude"],
"Longitude" => $row["longitude"],
"Teilnehmerzahl" => $row["Teilnehmerzahl"],
"Musikrichtung" => $row["Musikrichtung"],
"Art_Veranstaltung" => $row["Art_Veranstaltung"],
"Dauer_Veranstaltung" => $row["Dauer_Veranstaltung"]
));
}
echo json_encode($data);
Try this
$data = [];
while ( $row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);

Undefined variable: row

I got a function that requests some information from my database, puts it in an array and returns it. The sql-statement is correct.
When the current date is not found in the database, I would like to display an error on my website.
The error message, is added also in the return array.
First I check if the corresponding date exists in the database with a COUNT, if the count == 1, I get all the data from the database with another statement. If the count != 1, I put together an array hard code.
When a date is found in the database that is the same as the given parameter, the script works like a charm, but when I change the date in the database, the I get the following error:
Notice: Undefined variable: row in
/Applications/MAMP/htdocs/models/funcs.php on line 1257
Line 1252-1257:
while($stmt->fetch())
{
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
$data = $row;
Does anyone know what I'm doing wrong here? Thanks in advance!
The complete function:
function get_current_planning($date)
{
GLOBAL $mysqli, $db_table_prefix;
$stmt = $mysqli->prepare("SELECT COUNT(datum) FROM " . $db_table_prefix . "planning WHERE datum = '" . $date . "'");
$result = $stmt->execute();
print_r($result);
var_dump($result);
if($result == 1)
{
$stmt->prepare("SELECT
*
FROM " . $db_table_prefix . "planning
WHERE datum = '" . $date . "'");
$stmt->execute();
$stmt->bind_result($id, $datum, $mac, $ipad, $iphone, $imember, $applecare, $verkoop_ochtend, $verkoop_middag, $verkoop_avond, $vracht_ochtend, $vracht_middag, $vracht_avond, $service_ochtend, $service_middag, $service_avond, $werkzaamheden);
while($stmt->fetch())
{
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
$data = $row;
}
else
{
$row[] = array('id' => '', 'datum' => '', 'mac' => '', 'ipad' => '', 'iphone' => '', 'imember' => '', 'applecare' => '', 'verkoop_ochtend' => '', 'verkoop_middag' => '', 'verkoop_avond' => '', 'vracht_ochtend' => '', 'vracht_middag' => '', 'vracht_avond' => '', 'service_ochtend' => '', 'service_middag' => '', 'service_avond' => '', 'werkzaamheden' => '', 'error' => 'Er is geen planning gevonden voor de huidige datum!');
$stmt->close();
$data = $row;
}
return $data;
}
print_r($result) returns 1
var_dump($result) returns bool(true)
You're not fetch()ing the the result of your SELECT COUNT()... query. It looks to me like your while(...fetch()) loop in your second query is sometimes running zero times. This will result in the code beginning $row[] = getting run no times, which in turn will result in $row turning up undefined after the while(...fetch()) loop.
At any rate it's a bit wasteful to count the rows and then fetch them, with two consecutive queries. You may want to skip the SELECT COUNT query entirely. Instead, do this sort of thing:
$row = Array();
while($stmt->fetch()) {
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
if (0 == count($row)) {
/* deal with the no matching rows case */
}
$data = $row;
It looks like you haven't defined a variable $row. You need a $row=Array(); before you can start treating $row as an array.

Unable to insert data into database using codeigniter

I want to insert data into my database. I am using codeigniter framework to build my app.
When I click on submit button,It don't give any error just reload the same page and data in not inserted in database.
Following my code to insert data into database -
public function addSale($saleDetails = array(), $items = array(), $warehouse_id)
{
foreach($items as $data){
$product_id = $data['product_id'];
$product_quantity = $data['quantity'];
$this->updateProductQuantity($product_id, $warehouse_id, $product_quantity);
}
// sale data
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
if($this->db->insert('sales', $saleData)) {
$sale_id = $this->db->insert_id();
$addOn = array('sale_id' => $sale_id);
end($addOn);
foreach ( $items as &$var ) {
$var = array_merge($addOn, $var);
}
if($this->db->insert_batch('sale_items', $items)) {
return true;
}
}
return false;
}
Based on https://ellislab.com/codeigniter/user-guide/database/examples.html , it seems to me, that you are missing a line -> $this->db->insert('sales', $saleData); . You do have it inside an if statement, but if statement will not execute it as a code, but check if it returns true.
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
$this->db->insert('sales', $saleData);
This should work. You can also check if it succeeds like this ->
return ($this->db->affected_rows() != 1) ? false : true;

mysql_insert_id returning only 0

i am stuck to get mysql last increment id :(
i just tried mysql_insert_id but getting 0 instead of last increment id :(
Here is the array and insert fucntion call
$input_data = array(
'id' => '',
'booking_date' => $_POST['booking_date'],
'event_name' => $_POST['ev_name'],
'exb_comp_name' => $_POST['exb_comp_name'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'country' => $_POST['country'],
'tel1' => $_POST['tel1'],
'tel2' => $_POST['tel2'],
'email' => $_POST['email'],
'package' => $_POST['package'],
'stand_no' => $_POST['stand_no'],
'hall_no' => $_POST['hall_no'],
'area' => $_POST['area'],
'contact_per' => $_POST['contact_per'],
'desg' => $_POST['desg'],
'cell_no' => $_POST['cell_no'],
'url' => $_POST['url'],
);
echo get_insert_id('exb_reg',$input_data);
And here is the function code
function get_insert_id($table_name, $data){
global $connection;
$value_a = '';
$value_b = '';
foreach($data as $field => $values){
$value_a .= "`".$field."`,";
$value_b .= "'".$values."',";
}
$value_a = substr($value_a, 0, strlen($value_a)-1);
$value_b = substr($value_b, 0, strlen($value_b)-1);
$query = "INSERT INTO `$table_name` ($value_a) VALUES ($value_b)";
$result = mysqli_query($connection,$query);
$id = mysql_insert_id();
return $id;
}
i tried echo in function but at the end result is 0 :(
You've switched database libraries. Use mysqli_ throughout, it isn't compatible with the deprecated mysql_ library.
http://php.net/manual/en/mysqli.insert-id.php

Looping through results of a sql query

I have a query that returns multiple rows. I can't seem to find a way to store the rows in the $params array. Is there a way to loop throw and store each row in the $params variable
$aResult = $db->exec_sql($sql);
$params = array(
// where $aResult[o]'' would be row 1 [1] row 2 etc. //
'store_id' => $aResult[]['iStoreID'],
'user_id' => $aResult[]['iUserID'],
'store_name' => $aResult[]['cStoreName'],
'store_url' => $aResult[]['cStoreURL'],
'rid' => $aResult[]['cRID'],
'affiliate_id' => $aResult[]['iAffiliateID'],
'team_id' => $aResult[]['iTeamID'],
'bizOP' => $aResult[]['cDefaultBizOpp'],
'showBizOPp' => $aResult[]['iShowBizOppDropdown'],
'boPosting' => $aResult[]['iEnableBOPosting'],
'brandinglevel' => $aResult[]['iBrandingLevel']
);
thank you for your help
As simple as that:
$params = array();
foreach($aResult as $row) {
$params[] = array(
'store_id' => $row['iStoreID'],
'user_id' => $row['iUserID'],
'store_name' => $row['cStoreName'],
'store_url' => $row['cStoreURL'],
'rid' => $row['cRID'],
'affiliate_id' => $row['iAffiliateID'],
'team_id' => $row['iTeamID'],
'bizOP' => $row['cDefaultBizOpp'],
'showBizOPp' => $row['iShowBizOppDropdown'],
'boPosting' => $row['iEnableBOPosting'],
'brandinglevel' => $row['iBrandingLevel']
);
}
Without knowing the exact structure of the result array i guess you need something like this:
<?php
$params = array();
$mapping = array(
'store_id' => 'iStoredID',
'user_id' => 'iUserID',
// and so on...
);
foreach ($aResult as $row) {
$tempRow = array();
foreach ($row as $key => $value) {
$paramKey = isset($mapping[$key]) ? $mapping[$key] : $key;
$tempRow[$paramKey] = $value;
}
$params[] = $tempRow;
}
I use it like this
$aResult = mysql_query($sql);
while($data = mysql_fetch_array($result)) {
'store_id' = $aResult['iStoreID'];
}
At least that is the idea

Categories