Improve processing speed of web page in PHP Mysql - php

My website take too much to load.I want to show a table on top of page and on bottom of page.
To get the same result twice,I am using same query two times.What is the best way to fetch data only once so that we improve speed?
Here is my code AT TOP
$q_s ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res = $objApp->query($q_s);
while($rw = $objApp->row($res)){
//display my data here in div1
}
AT BOTTOM
$q_s2 ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res2 = $objApp->query($q_s2);
while($rw = $objApp->row($res2)){
//display my data here in div2
}
NOTE:DIV1 and DIV2 are two different html.Also i showed only simplest query,its too complecated in my case.
How can i improve it?Sorry i am new to PHP

Here is what I would do
$rows = array();
$q_s ="SELECT id,ref,name,description FROM TABLE WHERE status=2 AND is_archive=0
ORDER BY id DESC limit 5";
$res = $objApp->query($q_s);
while($row = $objApp->row($res)){
array_push($row, $rows);
}
You can use $data in a foreach loop for an example
foreach($rows as $row) {
echo $row['colname'];
}
Now you can use data anywhere, multiple times in the page without doing any more queries, hope this helps :)

Related

PHP postgresql select, getting id of the one that follows the current one

I am getting 10 rows with the highest ID from a table ...
$result = pg_query($dbconn, "SELECT w_news_id, name, w_newsnachricht, w_newsdatum FROM adempiere.w_news ORDER BY w_news_id DESC LIMIT 10");
... then I build 10 divs in a while loop:
while ($row = pg_fetch_row($result)) {
// building divs here
}
BUT I want to also include the name that belongs to the next w_news_id in that same div (as a "teaser" for the "next"-arrow). So I was thinking I have to run a second query with the ID that would be next in the loop.
How is the SQL syntax for that? Or is there maybe a better way to do this?
You can use the lead window function:
SELECT w_news_id, name, w_newsnachricht, w_newsdatum,
LEAD (name) OVER (ORDER BY w_news_id) AS next_name
FROM adempiere.w_news
ORDER BY w_news_id DESC
LIMIT 10
You can select one more record - LIMIT 11, instead of 10, and process it differently in PHP.
$result = pg_query($dbconn, "SELECT w_news_id, name, w_newsnachricht, w_newsdatum FROM adempiere.w_news ORDER BY w_news_id DESC LIMIT 11");
$i = 0;
while ($row = pg_fetch_row($result) and $i < 10) {
$i++;
// building divs here
}
// then process the arrow and teaser separately (if present)
if ($row = pg_fetch_row($result)) {
// show teaser div
}
UPDATE
The window function solution provided by Mureinik is better.

Sorting SQL query with the submitting of a form

I have a general understanding of what I would like to do but not sure how to write the SQL.
Users have the ability from changing the sort from ASC to DESC and increase the query limit from 5 to 10.
$result=$mysqli->query("SELECT * FROM table WHERE status = 3 ORDER BY name ASC LIMIT $start_from, 5");
the option box for asc/desc will end up being $sort_order
the option box for limit will be $limita
I tried to write it like
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.="LIMIT $limita, 5";}
if ($result = $mysqli->query($result)) {
while($row = $result->fetch_object()){
but the query ended up empty due to the query being split into different lines.
Any idea what I am doing wrong? ill post more code if needed but this is generally where my issue is.
Put space between status and LIMIT.
$result = "SELECT * FROM wp_pod_tbl_bars WHERE status = 3";
if(!empty($limita)){$result.=" LIMIT '".$limita."', 5";}
---------------^

How to "shift" query result one row further

Let's say I have something like this:
$sql = $con->query("SELECT * FROM Content ORDER BY Time DESC LIMIT 2");
while ($row = $sql->fetch()) {
echo $row['title'];
}
So this grabs the 2 latest entries in a table and then echoes the designated column. How can I take the 2nd and 3rd most recent entries from my table ignoring the first one?
Right now I'm thinking about setting the limit to 3 and somehow skipping the first result and grabbing the 2 remaining.
Try this :
LIMIT 1,2
1 => offset : From where to start(First one will be 0)
2 => number of records
$sql = $con->query("SELECT * FROM Content ORDER BY Time DESC LIMIT 1,2");
while ($row = $sql->fetch()) {
echo $row['title'];
}

How to show 5 post each page with next & previous button feature?

I have a page named 'job.php', currently this page is showing all posted job. But now I want to show only 5 latest posts. And if anyone want to check the previous posts, they can click next button thus more 5 posts will be seen. There should be a previous button too.
Following is my code:
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC");
$num_row = mysql_num_rows($result1);
while($row1 = mysql_fetch_array($result1)){
$cat=$row1['Category'];
$title=$row1['Title'];
echo "Job field: $cat<br/> Title: $title<br/>";
}
N:B: It's not pagination. I don't want to show page numbers, just want to show next & previous button.
There are 100s of articles available on the Internet
Create Awesome PHP/MYSQL Pagination
PHP / MySQL select data and split on pages
If you want to do on your own:
Use LIMIT keywords in your query.
Pass the page and the multiplier to the LIMIT.
Some code
<?php
$limit = 5;
$start = (int)(($page - 1 ) * $limit);
$page = mysql_real_escape_string($_GET["page"]);
$query = "SELECT * FROM `table` LIMIT $start, {(int)$page + $limit}"
?>
There are two ways to achieve this.
1) In the query itself by using LIMIT
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 1, 5");
2 ) By using loop
$i = 0;
while($row1 = mysql_fetch_array($result1)){
if($i < 5) {
$cat=$row1['Category'];
$title=$row1['Title'];
echo "Job field: $cat<br/> Title: $title<br/>";
$i++;
}
}
You can pass the current value to URL and get it back by using $_GET..
You can use this query:
Select * from table_name ORDER BY ID DESC LIMIT 5;
I feel its better to use pagination. If you want dont want to show page nos, better hide it or just modify the code. If you planning to do it manually its a bit messy
You can use LIMIT in you mysql query:
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC LIMIT 0, 5");

mysql multiple table query inside php loop

Faily new to php and mysql, this will probably seem very messy.
This is what I came up with:
$query = "show tables like 'whatever%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$sql=mysql_query("SELECT * FROM ". $row[0] ." WHERE a=(SELECT MAX(a)) AND b=(SELECT MAX(b)) AND c LIKE 'd%' ORDER BY date DESC LIMIT 1");
while($info=mysql_fetch_array($sql)){
echo "...";
}
}
I get the desired value from each table, so x results depending on the amount of tables. What I would like is to have the results of the queried tables but only show the top 10-5 ordered by date/time.
Is this possible with the current script? Is there an easier way (while, number of tables changing constantly)? Is this query method database intensif?
Cheers!
I call constantly changing number of tables having similar structure a design error. also query switch to
$sql=mysql_query("SELECT * FROM $tbl WHERE c LIKE 'd%' ORDER BY a DESC, b DESC, date DESC LIMIT 1");
is a little relief to database.

Categories