I am retrieving 5000 plus data in my MYSQL database. I takes 5-10 minutes to retrieve the data (it is only just local slower when over the network) is there a way to improve the speed without using a plugin?
$ContactID = $_GET["Contact"];
$sql = "SELECT * FROM tblContacts WHERE Coordinator = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while ($row = mysqli_fetch_array($result)) {
$supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$ar[] = array(
'ContactID' => $row['ContactID'],
'FileAs' => $row['FileAs'],
'FirstName' => $row['FirstName'],
'MiddleName' => $row['MiddleName'],
'LastName' => $row['LastName'],
'Position' => $row['Position'],
'Company' => $row['Company'],
'CompanyID' => $row['CompanyID'],
'ContactType' => $row['ContactType'],
'RetailerType' => $row['RetailerType'],
'PresStreet' => $row['PresStreet'],
'PresBarangay' => $row['PresBarangay'],
'PresDistrict' => $row['PresDistrict'],
'PresTown' => $row['PresTown'],
'PresProvince' => $row['PresProvince'],
'PresCountry' => $row['PresCountry'],
'Landmark' => $row['Landmark'],
'Telephone1' => $row['Telephone1'],
'Telephone2' => $row['Telephone2'],
'Mobile' => $row['Mobile'],
'Email' => $row['Email'],
'Employee' => $row['Employee'],
'Customer' => $row['Customer'],
'Coordinator' => $row['Coordinator'],
'ServerUpdate' => $supdate,
'MobileUpdate' => $mupdate
);
}
print json_encode($ar);
}
$ContactID = $_GET["Contact"];
$ar = array();
$sql = "SELECT ContactID,FileAs, FirstName ,MiddleName ,LastName ,Position ,Company ,CompanyID ,ContactType ,RetailerType ,PresStreet
,PresBarangay ,PresDistrict ,PresTown , PresProvince ,PresCountry ,Landmark ,Telephone1 , Telephone2 ,Mobile
,Email ,Employee ,Customer ,Coordinator FROM tblContacts WHERE Coordinator = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
$rowCount = 0;
while ($row = mysqli_fetch_array($result)) {
$supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$ar[$rowCount] = $row;
$ar[$rowCount]['ServerUpdate'] = $supdate;
$ar[$rowCount]['ServerUpdate'] = $mupdate;
$rowCount++;
}
print json_encode($ar);
}
You must declare the variable of array first in the top.
You can directly call from mysql what data needed. Hope can help.
It could be like this way.
$contactID = $_GET["Contact"];
$sql = "SELECT * FROM tblContacts WHERE Coordinator = '$contactID'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
$data = array();
while($row = mysqli_fetch_array($result)){
$row['supdate'] = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$row['supdate'] = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$data[] = $row;
}
print json_encode($data);
}
updated for multiple records
Related
I making a script to monitorize data from some VPSs, which is being stored in a MySQL database.
$result = mysql_query("SELECT * FROM data");
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'id' => $row['id'],
'hostname' => $row['hostname'],
'loadavrg' => $row['load average']
);
}
I would like to separase data by hostnames so I can read it like the following example:
foreach($data['sitename.com'] as $d)
echo $d['loadavrg'];
I already tried with the following code (just for testing), but didn't work:
$result = mysql_query("SELECT * FROM data WHERE hostname='sitename.com'");
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'sitename.com' => array(
'id' => $row['id'],
'loadavrg' => $row['load average']
)
);
}
I'm just missing the right way and syntax to achieve it :X
Every element should be added as subarray of 'sitename.com':
while ($row = mysql_fetch_array($result)) {
$data['sitename.com'][] = array(
'id' => $row['id'],
'loadavrg' => $row['load average']
);
}
Try this,
while ($row = mysql_fetch_array($result)) {
$hostname = $row['hostname'];
$data[$hostname][] = array(
'id' => $row['id'],
'loadavrg' => $row['load average']
);
}
I am trying to extract data from mysql database into a datatable using ajax, and php.
The code for my response.php file is below:
<?php
$result = mysql_query("select * from orders");
while ($row = mysql_fetch_array($result)) {
$data = array(
array(
'Name' => $row['jobnumber'],
'Empid' => $row['ID'],
'Salary' => $row['product']
)
);
}
$results = array(
"sEcho" => 1,
"iTotalRecords" => count($data),
"iTotalDisplayRecords" => count($data),
"aaData" => $data
);
/*while($row = $result->fetch_array(MYSQLI_ASSOC)){
$results["data"][] = $row ;
}*/
echo json_encode($results);
?>
Why is this only returning one result in my front end table?
http://orca.awaluminium.com/test.php
link above shows table.
You're replacing value of $data instead of pushing new rows in an array.
Change the following line.
$data = array(
array(
'Name'=>$row['jobnumber'],
'Empid'=>$row['ID'], 'Salary'=>$row['product']
)
);
To
$data[] = array(
'Name'=>$row['jobnumber'],
'Empid'=>$row['ID'], 'Salary'=>$row['product']
);
Also put $data=array(); before string while() looop.
You have to do foreach
while ($row = mysql_fetch_array($result)){
foreach($row as $a)
{$data[] = array(
array('Name'=>$a['jobnumber'], 'Empid'=>$a['ID'], 'Salary'=>$a['product']),
);
}
}
I am developing a webservice for and android app the webservice is in PHP fetching data from a MySQL database ... The problem that I am having is that some of the projects have multiple details but my code is only fetching one detail for every project. I am getting the data in JSON format.
You can check this Here
Here is my code
function requiredData(){
$db = $this->dbConnection();
//$sql = "SELECT * FROM projects JOIN project_details ON projects.project_id=project_details.project_id";
$sql = "SELECT * FROM projects";
$queryResult = $db->query($sql);
if($queryResult->num_rows > 0){
while($row = $queryResult->fetch_assoc()){
$pid = $row['project_id'];
$detailsql = "SELECT * FROM project_details WHERE project_id=$pid";
$sqlResult = $db->query($detailsql);
if($sqlResult->num_rows > 0){
while ($d = $sqlResult->fetch_assoc()){
$r = array(
"project_id" => $d['project_id'],
"project_detail" => array(
"work_done" => $d['project_detail'],
"payment_for_work" => $d['project_payment'],
"payment_status" => $d['project_payment_status'],
"detail_id" => $d['project_detail_id']
)
);
}
}
$results[$row['project_name']] = array(
"project_id" => $row["project_id"],
"project_start_date" => $row["project_start_date"],
"project_due_date" => $row["project_due_date"],
"project_currency" => $row["project_currency"],
"project_work_details" => $r
);
}
}
return $results;
}
Thanks in advance for your help
The issue is in the second while loop. You are assigning array to $r, and $r value overwrites every time.
So, I am assigning now array to an other array, So it will become 2 dimensional array, like this;
while ($d = $sqlResult->fetch_assoc()){
$r[] = array(
"project_id" => $d['project_id'],
"project_detail" => array(
"work_done" => $d['project_detail'],
"payment_for_work" => $d['project_payment'],
"payment_status" => $d['project_payment_status'],
"detail_id" => $d['project_detail_id']
)
);
}
Now you can use $r, it will have multiple details of the project.
I have the following code:
include "config.php";
error_reporting(0);
$idSuccess = 0;
if(isset($_POST['userName']))
{
$usr = $_POST['userName'];
$pwd = $_POST['userPwd'];
$sqlQ = "SELECT * FROM dr_users where dr_user_name='$usr' AND dr_user_pwd='$pwd' LIMIT 1";
$q = mysql_query($sqlQ);
if(mysql_num_rows($q) > 0)
{
$userO = mysql_fetch_assoc($q);
$userData = array('userID' => $userO['dr_user_id'], 'userName' => $userO['dr_user_name'], 'userPwd' => $userO['dr_user_pwd'], 'userEml' => $userO['dr_user_email'], 'privLvl' => $userO['dr_user_priv_level']);
$idSuccess = 1;
$uID = $userO['dr_user_id'];
$charQ = mysql_query("SELECT * FROM dr_chars WHERE dr_user_id='$uID'");
while($char = mysql_fetch_array($charQ))
{
$chars[] = $char;
$charID = $char['dr_char_id'];
$itemQ = mysql_query("SELECT * FROM dr_items WHERE dr_char_id='$charID'");
while($item = mysql_fetch_array($itemQ))
{
$itemData[] = array('itemID' => $item['dr_item_id'], 'itemName' => $item['dr_item_name'], 'itemDesc' => $item['dr_item_desc'], 'itemType' => $item['dr_item_type'], 'itemCost' => $item['dr_item_cost'], 'itemCurType' => $item['dr_item_cur_type'], 'itemAtk' => $item['dr_item_stat_atk'], 'itemDef' => $item['dr_item_stat_def'], 'itemEnd' => $item['dr_item_stat_end'], 'itemLuck' => $item['dr_item_stat_luck'], 'itemFileURL' => $item['dr_item_file_url'], 'itemStaff' => $item['dr_item_staff'], 'itemEquipped' => $item['dr_char_eqp'], 'spX' => $item['dr_static_pos_x'], 'spY' => $item['dr_static_pos_y']);
}
$charData[] = array('charID' => $char['dr_char_id'], 'charName' => $char['dr_char_name'], 'charRace' => $char['dr_char_race'], 'charLvl' => $char['dr_char_lvl'], 'charItems' => $itemData);
}
$resData = array('idSuccess' => $idSuccess, 'user' => $userData, 'chars' => $charData);
} else {
$resData = array('idSuccess' => $idSuccess);
}
echo json_encode($resData);
}
?>
I have successfully loaded character data from the table 'dr_chars' in my database. I'm trying to load the corresponding item data from the retrieved character ID and push it into the $charData array. So I can then easily encode & output it in a JSON format.
If my question isn't clear just ask and I'll try to explain the situation better. The JSON data is being outputted into flash for dynamic use.
I managed to fix this final code as follows:
<?php
include "config.php";
error_reporting(0);
$idSuccess = 0;
if(isset($_POST['userName']))
{
$usr = $_POST['userName'];
$pwd = $_POST['userPwd'];
$sqlQ = "SELECT * FROM dr_users where dr_user_name='$usr' AND dr_user_pwd='$pwd' LIMIT 1";
$q = mysql_query($sqlQ);
if(mysql_num_rows($q) > 0)
{
$userO = mysql_fetch_assoc($q);
$userData = array('userID' => $userO['dr_user_id'], 'userName' => $userO['dr_user_name'], 'userPwd' => $userO['dr_user_pwd'], 'userEml' => $userO['dr_user_email'], 'privLvl' => $userO['dr_user_priv_level']);
$idSuccess = 1;
$uID = $userO['dr_user_id'];
$charQ = mysql_query("SELECT * FROM dr_chars WHERE dr_user_id='$uID'");
$chars = array();
while($char = mysql_fetch_array($charQ))
{
$charID = $char['dr_char_id'];
$itemQ = mysql_query("SELECT * FROM dr_items WHERE dr_char_id='$charID'");
$items = array();
while($item = mysql_fetch_array($itemQ))
{
$items[] = array('itemID' => $item['dr_item_id'], 'itemName' => $item['dr_item_name'], 'itemDesc' => $item['dr_item_desc'], 'itemType' => $item['dr_item_type'], 'itemCost' => $item['dr_item_cost'], 'itemCurType' => $item['dr_item_cur_type'], 'itemAtk' => $item['dr_item_stat_atk'], 'itemDef' => $item['dr_item_stat_def'], 'itemEnd' => $item['dr_item_stat_end'], 'itemLuck' => $item['dr_item_stat_luck'], 'itemFileURL' => $item['dr_item_file_url'], 'itemStaff' => $item['dr_item_staff'], 'itemEquipped' => $item['dr_char_eqp'], 'spX' => $item['dr_static_pos_x'], 'spY' => $item['dr_static_pos_y']);
}
$chars[] = array('charID' => $char['dr_char_id'], 'charName' => $char['dr_char_name'], 'charRace' => $char['dr_char_race'], 'charLvl' => $char['dr_char_lvl'], 'charItems' => $items);
}
$resData = array('idSuccess' => $idSuccess, 'user' => $userData, 'chars' => $chars);
} else {
$resData = array('idSuccess' => $idSuccess);
}
echo json_encode($resData);
}
?>
Hi I have an error when I call a function.
"Warning: Illegal string offset 'id' in C:\xampp\htdocs\blog\posts.php
on line 28
2"
function:
function get_short_posts() {
$sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$timestamp = new DateTime($row['date']);
return array (
"id" => $row['id'],
"title" => $row['title'],
"content" => $row['content'],
"author" => $row['author'],
"date" => $timestamp->format('d-m-Y'),
"time" => $timestamp->format('H:i')
);
}
Call:
require_once "functions.php";
$_posts = get_short_posts();
foreach($_posts as $_post) {
echo $_post['id'];
}
You know you return after the first iteration in the get_short_posts function right, so the foreach will not work as expected.
Try:
<?php
function get_short_posts() {
$sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
$result = mysql_query($sql);
$return = array();
while($row = mysql_fetch_assoc($result)) {
$timestamp = new DateTime($row['date']);
$return[] = array (
"id" => $row['id'],
"title" => $row['title'],
"content" => $row['content'],
"author" => $row['author'],
"date" => $timestamp->format('d-m-Y'),
"time" => $timestamp->format('H:i')
);
}
return $return;
}
?>
<?php
require_once "functions.php";
foreach(get_short_posts() as $_post) {
echo $_post['id'];
}
?>
Also, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
function get_short_posts() {
$sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$timestamp = new DateTime($row['date']);
$data [] = array (
"id" => $row['id'],
"title" => $row['title'],
"content" => $row['content'],
"author" => $row['author'],
"date" => $timestamp->format('d-m-Y'),
"time" => $timestamp->format('H:i')
);
}
return $data;
}
you return data, so the loop stops, save your data in a array and return that array like abive code
Your code is wrong it should be as below, assuming the query is returning data as mentioned.
function get_short_posts() {
$sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
$result = mysql_query($sql);
$rows = array();
$return_data = array();
while($row = mysql_fetch_assoc($result)) {
$timestamp = new DateTime($row['date']);
$data = array (
"id" => $row['id'],
"title" => $row['title'],
"content" => $row['content'],
"author" => $row['author'],
"date" => $timestamp->format('d-m-Y'),
"time" => $timestamp->format('H:i')
);
$return_data[] = $data;
}
return $return_data ;
}
require_once "functions.php";
$posts = get_short_posts();
foreach($posts as $key=>$val) {
echo $val['id'];
}