My website is performing very slow and taking lot of time to load the data.
Its like 2-3 mins to load the data. Can you please suggest me how to make it fast. I am fetching data from multiple table. The database has many entries almost 25000 entries.
Below Is the Code I am currently using.
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<tbody>
<?php // get all state
$sql=" SELECT bm.bank_name,b.bank_ifsc,e.emp_id,e.emp_code,
e.first_name,e.middle_name,e.last_name,
e.active_status as emp_status,
e.account_no FROM tblemployee e
Left Join tblbank_mst bm on bm.bank_id=e.fk_bank_id
Left Join tblbank b on b.bank_ifsc_id=e.fk_bank_ifsc_id
where e.del_status=0
and e.role_id=4
and e.is_admin=0 ";
if($_SESSION["loggedin_role_id"]==2)
{
$sql.=" and e.added_by=".$_SESSION["loggedin_emp_id"];
}
$sql.=" order by first_name";
// echo $sql; exit;
if(mysql_num_rows($result = mysql_query($sql))>0)
{
while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td><?php echo $row['emp_code'];?> </td>
<td><?php echo $row['first_name'].' '.$row['middle_name'].' '.$row['last_name'];?> </td>
<td><?php echo $row['bank_name'];?> </td>
<td><?php echo $row['account_no'];?> </td>
<td><?php echo $row['bank_ifsc'];?> </td>
<td class="center">
<?php if($row['emp_status']==1){ ?>
<span class="label label-success">Active</span>
<?php }else{?>
<span class="label label-danger">Inactive</span>
<?php }?>
</td>
<td class="center">
<!--<a class="btn btn-success" href="#">
<i class="halflings-icon white zoom-in"></i>
</a>-->
<?php if($row['approve_status']==0){ ?>
<a class="btn btn-info" href="edit_employee.php?emp_id=<?php echo $row['emp_id'];;?>">
<i class="halflings-icon white edit"></i>
</a>
<a class="btn btn-danger" href="#" onClick="ConfirmDelete(<?php echo $row['emp_id'];?>)">
<i class="halflings-icon white trash"></i>
</a>
<?php }else{ echo "--"; }?>
</td>
</tr>
Assuming your largest table is tblemployee, try creating a compound index on the three columns mentioned in your WHERE clause, which is:
WHERE e.del_status=0 and e.role_id=4 and e.is_admin=0
You can do this with
CREATE INDEX emp_del_role_admin
ON tblemployee
(del_status, role_id, is_admin);
Why does this help? Because MySQL's query planner can random-access the index to find the first row of your table matching your WHERE statement, then it can read the index sequentially to find the rest of the matching rows.
Of course, if your WHERE filter matches many thousands of rows in your table, you will still have a slow page; it takes time to load, transmit, and render a very large page.
If you are trying to display 25000 (or too many) entries in one time :
This is slow and that's normal, you should paginate your results : use some infinite scroll plugin to display the same thing and limiting results for each query -EDIT : or DataTable pagination options-.
If you are not
First you should have a look at slow queries , configuration to change in my.cnf file.
If your query is slow, you may then optimize your query by adding INDEX where you have to. You should use EXPLAIN (documentation) to help you doing this.
Be sure you have foreign keys declared as foreign keys (bm.bank_id & e.fk_bank_id - b.bank_ifsc_id & e.fk_bank_ifsc_id), that will speed up your query. Adding index on things like e.role_id could do it aswell.
Only you can know which index to add in this case.
Do not use PHP's mysql_* interface; switch to either mysqli_* or PDO.
Add two composite indexes:
(del_status, role_id, is_admin, first_name)
(del_status, role_id, is_admin, added_by, first_name)
The first handles the case where you skip the additional AND.
The following pattern may help performance more: Change
... bm.bank_name,
...
Left Join tblbank_mst bm ON bm.bank_id=e.fk_bank_id
to
... ( SELECT bank_name FROM tblbank_mst WHERE bank_id=e.fk_bank_id
) AS bank_name,
...
Ditto for the other LEFT JOIN.
Please see this answer as it might help you:
https://stackoverflow.com/a/35757120/1276062
In short you should check if you have indices on theese columns:
tblbank_mst.bank_id
tblemployee.fk_bank_id
tblbank.bank_ifsc_id
tblemployee.fk_bank_ifsc_id
tblemployee.del_status
tblemployee.role_id
tblemployee.is_admin
tblemployee.added_by
first_name
If the indices won't help you should run EXPLAIN on the query and post the results into the question
Related
Hey there guys/girls I have an issue I'm currently trying to work through being a novice to MYSQL / PHP. Currently I'm using Bootstrap accordion collapsible components to display HTML tables (That are reports). Here is my current table:
Current Table in MYSQL.
So as you can see the reports row contains some HTML information which are tables. I wanted to take the information and display it on a webpage assuming that every row was a different report. So I was able to do so with writing this:
<div class="accordion" id="accordionExample">
<?php
require('db.php');
$i = 0;
$sql = "SELECT `report` FROM `automation-reports`;";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($query))
{
foreach($row as $key => $value)
{
?>
<div class="card">
<div class="card-header" id="heading<?php echo $i ?>">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse<?php echo $i ?>" aria-expanded="true" aria-controls="collapse<?php echo $i ?>">
Report #1: 8/6/2018
</button>
</h5>
</div>
<div id="collapse<?php echo $i ?>" class="collapse" aria-labelledby="heading<?php echo $i ?>" data-parent="#accordionExample">
<div style="text-align: center;" class="card-body">
<h3 style="float: left;"> Rating-Pull: </h3>
<?php
$i++;
echo $key;
echo "$value";
?>
</div>
</div>
</div>
<?php
}
}
?>
</div>
Which is great because it does what I thought I wanted it to do , which is this:
Display Output
What's not so great is now I realize that multiple reports are going to be in one accordion "folder" which is where the reportid row comes into play. So lets say I run my program and two (different) reports run on it but I want it in the say "folder" on the webpage. Both of these get labeled with a reportid of 1.
So what I want to do is loop through reports and then if they have the same ID group them together in that folder and iterate through the whole table like that. So that's the part where I have attempted to do so with a nested loop and SELECT 'report' FROM 'automation-reports' WHERE 'reportid' = '$i' ; and I just ended up getting the first element. Could somebody give me a hand with this and a good explanation so I can understand and learn what's happening?
Thank you!
EDIT:
Maybe a visual would be better?
VISUAL
I think GROUP BY and GROUP_CONCAT are what you are looking for.
SELECT `reportid`, GROUP_CONCAT(`report` SEPARATOR '') as report
FROM `automation-reports`
GROUP BY `reportid`
shoud do the job.
SELECT *
FROM `automation-reports`
GROUP BY `reportid`
will give you one row for each id ie.
id 1,
id 2,
id 3
Or do you want to display each row like so?
id 1, id 1,
id 2,
id 3, id 3,
id 4
if so here is a possible example of combining the reports in a loop first
$sql = "SELECT reportid, GROUP_CONCAT(report SEPARATOR ',') as reports FROM `automation-reports` GROUP BY `reportid`;";
while($row = mysqli_fetch_assoc($query)) {
echo "<div id='{$row['reportid']}'>";
echo $row['reports'];
echo "</div>";
}
I know this isn't the HTML you're after but you should be able to place your HTML in this code
$query="SELECT data_home.name_home, flower.flower FROM
data_home INNER JOIN (flower INNER JOIN home_flower ON flower.id_flower=home_flower.id_flower)
ON data_home.id_home = home_flower.id_home";
$data=mysql_query($query);
$no=0;
//output
while ($row=mysql_fetch_array($data))
{
$no++;
if (empty($row['foto_home']))
{
$gambar = "default2.jpg";
}
else
{
$gambar = $row['foto_home'];
}
echo "<tr class='odd gradeA'>
<td width='5%'>".$no."</td>
<td width='20%'>".$row['name_home']."</td>
<td width='25%'>".$row['home_adress']."</td>
<td width='15%'>".$row['flower']."</td>
<td width='15%'><img src='uploads/".$gambar."' width='125px' height='125px'></img></td>
<td width='20'>
<center>
<a href='?restore=".$row['id_home']."' onclick='return confirmRestore()'>
<button type='button' class='btn btn-warning btn-circle' title='Restore'><i class='icon-refresh'></i><font size='1px'> Restore</font></button>
</a>
<a href='?delete=".$row['id_home']."' onclick='return confirmSubmit()'>
<button type='button' class='btn btn-danger btn-circle' title='Hapus'><i class='icon-trash'></i><font size='1px'> Delete</button></font>
</a>
</center>
</td>
</tr>
";
}
?>
i have code like this for my program and The output I want is as follows:
no home flower
1 type 41 tulip, rose, lily
but i dont knnow why when i running the program the out come be like this:
no home flower
1 type 41 tulip
2 type 41 rose
3 type 41 lily
i am really new to php and html so i dont have idea how to fix this.my question how to arrange it so "flower" with same "home" just count 1 and just seperate flower in one column with "," ?.thnx and sory for my bad english and for mysql code iknow ppl today using mysqli but this is the old program.
Your expected output in SQL could be achieved by this query, which uses GROUP_CONCAT function of MYSQL and gives you comma separated flowers, aggregated by home
But if you current query you haven't mentioned about column no.
SELECT dh.name_home, group_concat(f.flower) as flower
FROM
data_home dh
INNER JOIN
home_flower hf
ON dh.id_home = hf.id_home
INNER JOIN
flower f
ON f.id_flower=hf.id_flower
group by dh.name_home
Use this query.
$query="SELECT data_home.name_home, flower.flower FROM
data_home INNER JOIN (flower INNER JOIN home_flower ON flower.id_flower=home_flower.id_flower)
ON data_home.id_home = home_flower.id_home
GROUP BY data_home.name_home, flower.flower
"
I would like to display three images from the database in one row. At the moment, all the images are in a vertical line and I do not know how to fix it. Any help would be appreciated. I am only learning PHP, have checked other questions and they have been no help.
I have tried using Bootstrap however that has not worked either. The way I have it does display 3 in a row however, shoes the same product in each row and only if I have the query $show_men="SELECT model, id, price,image FROM products WHERE id='id' "
<div class="col-xs-12 col-sm-4">
<!--selecting products from database and displaying. when user clicks on more info button, information is stored in array and displayed on item.php-->
<?php
include 'mysql.php';
$show_men="SELECT model, id, price,image FROM products WHERE cat='men' ";
$query_men=mysqli_query($conn,$show_men);
while($row=mysqli_fetch_array($query_men,MYSQLI_ASSOC)){
echo '<br><br><table class="table"><tbody>
<tr>
<div><center><img class="img-responsive" src="../images/'.$row['image'].'" width="200" height="200"></center><br>
<center><strong>'.$row['model'].'</strong><br><br><strong>Price:£ '.$row['price'].'</strong><br><br>
More Info<br><br><strong></center></div><hr>
</tr>
</tbody>
</table>';
}?>
</div>
You need to iterate the column instead of the table..
<div class="row">
<?php
include 'mysql.php';
$show_men="SELECT model, id, price,image FROM products WHERE cat='men' ";
$query_men=mysqli_query($conn,$show_men);
while($row=mysqli_fetch_array($query_men,MYSQLI_ASSOC)){
echo '<div class="col-xs-12 col-sm-4"><br><br><table class="table"><tbody>
<tr>
<div><center><img class="img-responsive" src="../images/'.$row['image'].'" width="200" height="200"></center><br>
<center><strong>'.$row['model'].'</strong><br><br><strong>Price:£ '.$row['price'].'</strong><br><br>
More Info<br><br><strong></center></div><hr>
</tr>
</tbody>
</table></div>';
}
?>
</div>
I made code which loads online players from game server(through MySQL table). Now, I'm newbie in PHP and I don't Know how to sort them(users) in table by ID. I googled and I found answer which will sort arrays by value(1, 2, 3, 4 etc..). Only problem is because users, with ID, have a name. How to connect user's name with ID, so they stay together after sort?
Here's a code
while($_hsync_podatci = $_hsync_rezultat->fetch_assoc())
{
?>
<tr class="_hsync_online_stil_<?php echo $_hsync_dio; ?>" id="_hsync_na_mrezi_<?php echo $_hsync_podatci['ServerID']; ?>">
<td><?php echo $_hsync_podatci['ServerID']; ?></td>
<td><?php echo $_hsync_podatci['Ime']; ?></td>
<td>
<button type="button" id="_hsync_izbaci_<?php echo $_hsync_podatci['ServerID']; ?>" class="btn btn-danger" onclick="_hsync_izbaci(<?php echo $_hsync_podatci['ServerID']; ?>)" style="float: right;">
<span class="glyphicon glyphicon-log-out"></span>Izbaci
</button>
</td>
</tr>
<?php
$_hsync_dio = !$_hsync_dio;
}
Above while is table's header. Var $_hsync_dio is for background color. One row is white, second is grey, and so on.
When doing a MySQL query you can sort the array automatically when fetching the data for example:
$players = mysqli_query($conn, "SELECT * FROM playerTable ORDER BY id ASC");
while($row = mysqli_fetch_assoc($message_sender_info)){
$message[] = $row;
}
Use DESC instead of ASC for e reverse sorting.
I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.