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.
Related
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);
I am trying to create an array of arrays in a loop to a JSON object, but It returns two objects instead. If I remove the array around the array with a key, it works but I need the key.
This is the format I am looking for:
$shop = array( "1408842145690" => array( id => "1408842145690",
code => "1",
title => "zdfdsf",
date => "2014-08-01",
description => "fghgf"
),
"1408840099517" => array( id => "1408840099517",
code => "1",
title => "test",
date => "2014-08-01",
description => "this is a test"
)
);echo json_encode($shop);
This is the code I am using
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array(
$row['task_id'] => array( 'id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
)
);
$alltasks[] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}
This is the result I get:
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01 00:00:00","description":"fghgf"}},{"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01 00:00:00","description":"this is a test"}}
This is the result I am looking for
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01","description":"fghgf"},"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01","description":"this is a test"}}
Replace the contents of your while loop with the following:
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
EDIT: I tested the above, and it does work. Here is the full code with the replacement intact... try a copy/paste.
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}
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
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
I am needing to create a multidimensional array with sql and loops. However only one result gets set into the array, the last result is overwriting the previous results. Here is what the array structure looks like:
->value = Array (4)
CartID => "1299"
Date => "2012-09-27 09:17:20"
Amount => "85.00"
0 => Array (8)
CartStatus => "Purchased"
Date => "2012-09-27 09:17:20"
CartID => "1299"
Sequence => "1"
Amount => "-85.00"
Comments => " , Refund Status: "
Here is my code:
$txarray = array();
foreach ($data as $transaction) {
$CartVar = $transaction['CartID'];
$CartStatus = $transaction['Status'];
$CartDate = $transaction['DateTime'];
$CartTotal = $transaction['Total'];
$txarray = array('CartID' => $CartVar, 'Date' => $CartDate, 'Amount' => $CartTotal);
$sql1 = $db->query("SQL stuff");
foreach ($sql1 as $refund) {
$CartID = $refund['CartID'];
$Sequence = $refund['Sequence'];
$TrxType = $refund['TrxType'];
$ParentID = $refund['ParentID'];
$TotalSum = '-'.$refund['Amount'];
$Comments = ' '.$refund['Comments'];
$Comments .= ', Refund Status: '.ucwords($refund['Status']);
$txarray[] = array('CartStatus' => $CartStatus, 'Date' => $CartDate, 'CartID' => $CartID, 'Sequence' => $Sequence, 'TrxType' => $TrxType, 'ParentID' => $ParentID, 'Amount' => $TotalSum, 'Comments' => $Comments);
}
Looking at above code snippet $txarray variable is overwritten due to code.
$txarray = array('CartID' => $CartVar, 'Date' => $CartDate, 'Amount' => $CartTotal);
It needs to be replaced it with
$txarray[] = array('CartID' => $CartVar, 'Date' => $CartDate, 'Amount' => $CartTotal);
or alternatively you can use array_push() function which will push arrays thereby resulting into multidimensional array.
e.g.
array_push($txarray,array('CartID' => $CartVar, 'Date' => $CartDate, 'Amount' => $CartTotal));
For more documentation about array_push function please refer the documentation in below mentioned url.
http://php.net/manual/en/function.array-push.php
This line:
$txarray = array('CartID' => $CartVar, 'Date' => $CartDate, 'Amount' => $CartTotal);
destroys any previous array'd you'd created in earlier iterations.