PHP - mysqli_fetch_assoc, 2 results then into an array - php

$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` =' $caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$selectganadores3 = $selectganadores2['usuario'];
array_push($arrayresultados,$selectganadores3);
}
Why the results are not pushing into the array? I'm new with Programming, sorry for my errors.

Try it with $arrayresultados[]
$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` ='$caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$arrayresultados[] = $selectganadores2['usuario'];
}

Related

JSON encoding not working

Here is my code to encode data in JSON format, but it doesn't work. The result is []. Where is my mistake?
<?php
$conn = new mysqli('localhost','root','','project');
$data =array();
if(!empty($_GET['masp'])){
$masp =$_GET['masp'];
$sql ="SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn,$sql);
if($result){
while($r = mysqli_fetch_assoc($result)){
$r['masp'] =$data['masp'];
$r['loai'] =$data['loai'];
$r['hangsx']=$data['hangsx'];
$r['tensp']=$data['tensp'];
$r['img']=$data['img'];
$r['gia']=$data['gia'];
$r['nx']=$data['nx'];
}
}
}
print json_encode($data);
?>
You are setting your variables wrong.
In every while cycle you get a new $r variable that you want to add to your $data variable.
$conn = new mysqli('localhost', 'root', '', 'project');
$data = array();
if (!empty($_GET['masp'])) {
$masp = $_GET['masp'];
$sql = "SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn, $sql);
$i = 0;
if ($result) {
while ($r = mysqli_fetch_assoc($result)) {
$data[$i]['masp'] = $r['masp'];
$data[$i]['loai'] = $r['loai'];
$data[$i]['hangsx'] = $r['hangsx']];
$data[$i]['tensp'] = $r['tensp'];
$data[$i]['img'] = $r['img'];
$data[$i]['gia'] = $r['gia'];
$data[$i]['nx'] = $r['nx'];
$i += 1;
}
}
}
print json_encode($data);
You make mistake. You should swap variable data with r inner loop, but probably than also will works unpropely. write in while loop $data [] = $r;

PHP Fusing two arrays

How to connect 2 arrays? I want that $new[$code]=$color, how can I do this? Below is my code:
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}
Declare the variable outside the while loop
$new = array();
Then inside while loop
$new[$row['user_id']] = $row['user_color'];
In the while loop...
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$new[$row['user_id']] = $row['user_color'];
}
If you need the arrays seperate for some reason you can do it later using array_combine, http://php.net/manual/en/function.array-combine.php.
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}
...
$new = array_combine($code, $color);

This page is taking too long to load

I have created a view page on php zend framework. View page has 10 methods. Each method has 3 sql queries returning thee values. I am calling these 10 methods 10 times. All queries is working on the table of 24,000 rows. It's taking 2-3 minutes to load the view page. I am storing the function results in arrays and using the array values to display.
How can I implement output caching on this?
Source code for my view part is:
Those are my variables:
$no_of_customers = array();
$no_of_customers_EE = array();
$no_of_customers_NEE = array();
$unique_customers = array();
$unique_customers_EE = array();
$unique_customers_NEE = array();
$no_of_sent = array();
$no_of_sent_EE = array();
$no_of_sent_NEE = array();
$no_of_opened = array();
$no_of_opened_EE = array();
$no_of_opened_NEE = array();
$no_of_surveys = array();
$no_of_surveys_EE = array();
$no_of_surveys_NEE = array();
$promoter = array();
$promoter_EE = array();
$promoter_NEE = array();
$detractor = array();
$detractor_EE = array();
$detractor_NEE = array();
$passive = array();
$passive_EE = array();
$passive_NEE = array();
one of the function i am using is
function unique_customer($con,$start_date,$end_date ,&$unique_customers,$i,$customerTable,&$unique_customers_EE,&$unique_customers_NEE)
{
$result = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' ;");
$rs = mysqli_fetch_array($result);
$unique_customers[$i] = $rs['Total'];
unset($rs);
$result_EE = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total_EE, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' AND product = '';");
$rs_EE = mysqli_fetch_array($result_EE);
$unique_customers_EE[$i] = $rs_EE['Total_EE'];
unset($rs_EE);
$result_NEE = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total_NEE, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' AND product != '';");
$rs_NEE = mysqli_fetch_array($result_NEE);
$unique_customers_NEE[$i] = $rs_NEE['Total_NEE'];
unset($rs_NEE);
}
Calling these functions 10 times and storing the values in arrays

HighCharts Data and Legend Ordering

I set up a HighChart to look at a MySQL database via a JSON feed, however the problem I have is that if the stacked column chart displays January to December from left to right the Legend shows it in reverse order December to January left to right.
Having tried to re-order the series in the code and poking around on the HighCharts information I am at a dead end.
Is this correctable, and if so could you let me know what to change please? Thanks.
Here's my code:
<?php
$con = mysql_connect("***","***","***");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("***", $con);
$query = mysql_query("SELECT * FROM viewByMonth");
$category = array();
$category['name'] = 'variety';
$series1 = array();
$series1['name'] = 'Jan';
$series2 = array();
$series2['name'] = 'Feb';
$series3 = array();
$series3['name'] = 'Mar';
$series4 = array();
$series4['name'] = 'Apr';
$series5 = array();
$series5['name'] = 'May';
$series6 = array();
$series6['name'] = 'Jun';
$series7 = array();
$series7['name'] = 'Jul';
$series8 = array();
$series8['name'] = 'Aug';
$series9 = array();
$series9['name'] = 'Sep';
$series10 = array();
$series10['name'] = 'Oct';
$series11 = array();
$series11['name'] = 'Nov';
$series12 = array();
$series12['name'] = 'Dec';
while($r = mysql_fetch_array($query)) {
$category['data'][] = $r['variety'];
$series1['data'][] = $r['Jan'];
$series2['data'][] = $r['Feb'];
$series3['data'][] = $r['Mar'];
$series4['data'][] = $r['Apr'];
$series5['data'][] = $r['May'];
$series6['data'][] = $r['Jun'];
$series7['data'][] = $r['Jul'];
$series8['data'][] = $r['Aug'];
$series9['data'][] = $r['Sep'];
$series10['data'][] = $r['Oct'];
$series11['data'][] = $r['Nov'];
$series12['data'][] = $r['Dec'];
}
$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
array_push($result,$series3);
array_push($result,$series4);
array_push($result,$series5);
array_push($result,$series6);
array_push($result,$series7);
array_push($result,$series8);
array_push($result,$series9);
array_push($result,$series10);
array_push($result,$series11);
array_push($result,$series12);
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
You can reverse legend order by parameter: reversed

Removing double qoute in json_encode / PHP

I need some help with JSON and PHP. Here's my code in PHP:
include 'class.Connection.php';
$branch = $_GET["b"];
$records = array();
$sqlNailDisplay = "SELECT NAD_ID FROM tbl_NailArtDesign WHERE NAD_Available = 1";
$query0 = mysql_query($sqlNailDisplay) or die(mysql_error());
while($rSet0 = mysql_fetch_array($query0, MYSQL_BOTH)) {
$actualPrice = 0.00;
$nailart = $rSet0["NAD_ID"];
//please note, { is the ascii code for '{', } is the ascii code for '}', while " is the ascii code for '"'
$mergedData = "{"NAD_ID":"".$nailart."","";
//individual nail art details
$sqlNailArt = "SELECT * FROM tbl_NailArtDesign WHERE NAD_ID = '".$nailart."' AND NAD_Available = 1";
$query1 = mysql_query($sqlNailArt) or die(mysql_error());
while($rSet1 = mysql_fetch_array($query1, MYSQL_BOTH)) {
$NAD_Ext = $rSet1["NAD_Ext"];
$CC_ID = $rSet1["CC_ID"];
$CT_ID = $rSet1["CT_ID"];
$CST_ID = $rSet1["CST_ID"];
if(empty($CST_ID)) {
$CST_ID = "null";
}
$NAD_Descrip = $rSet1["NAD_Descrip"];
$mergedData = $mergedData."NAD_Ext":"".$NAD_Ext."","CC_ID":"".$CC_ID."","CT_ID":"".$CT_ID."","CST_ID":"".$CST_ID."","NAD_Descrip":"".$NAD_Descrip."","";
}
//product used and price details
$sqlProductsUsed = "SELECT PL_ID FROM tbl_ProductUsed WHERE NAD_ID = '".$nailart."'";
$query2 = mysql_query($sqlProductsUsed) or die(mysql_error());
while($rSet2 = mysql_fetch_array($query2, MYSQL_BOTH)) {
$PL_ID = $rSet2["PL_ID"];
$sqlProductPrice = "SELECT PP_Amount FROM tbl_ProductPrice WHERE PL_ID = ".$PL_ID." AND BL_ID = '".$branch."'";
$query3 = mysql_query($sqlProductPrice) or die(mysql_error());
while($rSet3 = mysql_fetch_array($query3, MYSQL_BOTH)) {
$price = number_format($rSet3["PP_Amount"],2);
$actualPrice = number_format($actualPrice + $price,2);
}
$mergedData = $mergedData."PL_ID":"".$PL_ID."","PP_Amount":"".$price."","";
}
$mergedData = $mergedData."NAD_Price":"".$actualPrice.""}";
$records[] = $mergedData;
} mysql_free_result($query0);
echo json_encode($records);
And this is the result I'm getting:
["{"NAD_ID":"ND0001","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Giving you the aquatic feeling with Turquoise Marble","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}","{"NAD_ID":"ND0002","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Add a twirl in your life with Lavender Twirl","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}"]
I need my result to look like this:
[{"NAD_ID":"ND0001","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Giving you the aquatic feeling with Turquoise Marble","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"},{"NAD_ID":"ND0002","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Add a twirl in your life with Lavender Twirl","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}]
There an extra double quotes that I need to remove from my output.
["{" , ",*"* , }"]
Please help, I'm already at my limit and I already did searching for this, and I can't seem to get any resolution for this...
Hardcoded method:
$result = "[".substr(json_encode($records), 2, -2)."]";
$result = str_replace('","', ',', $result);

Categories