combining two While & sql statments - php

I need to combine the two queries if possible or make them process one after the other. I'm assuming the $Record_Count = $Record_Count + 1; doesn't need to be there twice since that's just for the pagination script. (thanks in advance)
$results = mysql_query("SELECT * load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
while ($data = mysql_fetch_array($results)) {
$Record_Count = $Record_Count + 1;
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
while ($recdata = mysql_fetch_array($rec_res)) {
$Record_Count = $Record_Count + 1;

IF $Record_Count is just counting the number of returned rows you could always use mysql_num_rows()
$results = mysql_query("SELECT * FROM load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
$Record_Count += mysql_num_rows($result) + mysql_num_rows($rec_res);

Related

remove duplicate camma from mysql query

I am trying to fetch data with mulyiple value with Mysql IN operator but problem I am getting to much (,,,).
Here is my query :
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
$rel_b.= $page_id['rel_blog'];
}
$p_id."<br>";
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
Ouery output is Like this this is not proper form so please help me .
SELECT * FROM pages WHERE rel_blog IN
(,114,93,126108default,,,,default,,,,default,,default,,,default,,default,,,,,,,default,,default,,,default,,default,,,,,,,,,,,,,,,,,,,,,,,,,,123,145,144,148,81,107default,,,,,default,,,,,,145,138,118default,,,,,,default,,,,,,,default,,,,,,,default,,,,,,,default,,,118default,,,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,default,,,,,,default,,,,,,,,,default,,,,,,,,default,,,,,default,,,,,,,123,122,125,113default,,,default,,,default,,124,145140default,,,,,,,,,,,,,default,,,,,,,,default,,,default,,,default,,,,default,,,,default,,default,,,,default,,,default,,,default,,default,,,,,,,default,,,,,,default,,,,default,,,,,,default,,,default,,,,137default,,,,,,,default,,,default,,,default,,,,,,default,,,,,,,,,,default,,,,,,,,82,83default,,,,,,default,,,,default,,,,default,,,default,,,default,,,,default,,,,default,,,,default,,,,default,,,default,,,,default,,,,default,,,default,,,default,,,,,,default,,,,,,default,,,,,default,,,default,,,,,default,,,,,default,,,,default,,,default,,default,,default,,default,,,,,,,default,,,,default,,default,,,,default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,,,,default,,,,default,,,,,default,,,,,,default,,,,,,,default,,,,,,default,,,,,,,default,,default,,default,,,,,default,,,default,,,default,,,default,,,,default,114,default,,,,,,117,116default,,,,,default,,,,117,116default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,default,,,default,,default,,,,,,default,,,,,,default,,,,,108default,,,,118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,142,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138,89,126,113,default,default,118,146,82,81,148,107,142,115,109,126,default,123,144,145,80,146,82,81,117,142,128,138,135,93,140,123,144,145,132,82,80,117,83,142,130,140,145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,123,144,118,80,82,81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,134,139,113,141,124,93140,150,150)
order by add_date desc limit 7
Actually I can't figure out how you are getting even a single comma... This is how you can try it out.
$get_id = "select rel_blog,page_id from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") {
$rel_b.= $page_id['rel_blog'] .",";
}
}
$p_id."<br>";
$rel_b = substr($rel_b,0,-1); // get rid of the last comma
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
You can do this before your query.
$rel_b = preg_replace('/,+/', ',', trim($rel_b, ','));
This will remove repeated commas as well as commas from beginning and end of the string.
Try using implode():
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id = array();
$rel_b = array();
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id[] = $page_id['page_id'];
$rel_b[] = $page_id['rel_blog'];
}
$p_id = implode(',', $p_id);
$rel_b = implode(',', rel_b);
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";

PHP MySQL Results num total rows and split for pages

Is there any way to count and split results without doing 2 query,
im using a query something like this:
$result = mysqli_query($con,"SELECT * from articles WHERE category = '$category'");
$row = mysqli_fetch_row($result);
$rows = $row[0];
$page_rows = 20;
$last = ceil($rows/$page_rows);
$pagenum = 1;
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result2 = mysqli_query($con,"SELECT * FROM articles order by id desc $limit");
while($row = mysqli_fetch_array($result2)) {
$id = $row['id'];
}
this is working but i dont like that it has 2 queries, any better idea? thank you
$pagenum = 1;
$rows_on_page = 20;
$start = (($pagenum - 1) * $rows_on_page);
$end = ($pagenum * $rows_on_page);
$result = mysqli_query($con, "SELECT * from articles WHERE category = '$category' ORDER BY id DESC LIMIT $start, $end");
while ($row = mysqli_fetch_array($con,$result) {
... do stuff with articles ...
$pagenum++;
}
The while loop will protect you from going past the end of the records.
try this query,it will return count of records and pagination (title in query is a field name, change it based on your table):
SELECT aa.countt, title FROM articles , (SELECT COUNT(*) AS countt FROM articles WHERE category = '$category' ) AS aa ORDER BY id LIMIT 5,10

Need help in displaying last 5 rows. PHP, MYSQL

Can anyone tell me what am I doing wrong. I would like to display the last 5 rows in desc order.
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id desc limit 0,5")
or die(mysql_error());
while($row = mysql_fetch_array( $pull_activity_logs )) {
$activity_time = $row["datetime"];
$activity = $row["activity"];
}
echo "$activity";
Help would be deeply appreciated
Well, you can always select the first five in asc order, that would be last five in desc order, and then you could reverse their order in php if needed (5 values isnt anything what an array couldn't handle)
CODE:
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id asc limit 5");
$temp_arr = array();
while($row = mysql_fetch_array( $pull_activity_logs )) {
$temp_arr[] = $row; //add to end
}
$final_arr = array_reverse($temp_arr);
foreach($final_arr as $row) //this doesn't have to be named $row
$activity_time = $row["datetime"];
$activity = $row["activity"];
echo "$activity";
}
EDIT:
now when i look at it maybe whole problem was in wring position of your echo:
$pull_activity_logs = mysql_query("SELECT * FROM activity_logs WHERE ac_no = '$logined_acc' order by id desc limit 0,5")
or die(mysql_error());
while($row = mysql_fetch_array( $pull_activity_logs )) {
$activity_time = $row["datetime"];
$activity = $row["activity"];
echo "$activity"; //this goes INSIDE the loop
}
You can retrieve the first five in ascending order and then order them in SQL by using a subquery:
select al.*
from (SELECT al.*
FROM activity_logs al
WHERE ac_no = '$logined_acc'
order by id asc
limit 0, 5
) al
order by id desc;

PHP/MySQL using ORDER BY with a LIMIT

I created a pagination by roughly following this link:
http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle
quite cool. Although I have an issue with my query.
It works fine like this:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ");
while ($row = mysql_fetch_assoc($query)) {
However when I try to add an ORDER BY to this, like so:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ORDER BY model DESC");
while ($row = mysql_fetch_assoc($query)) {
or just change the statement like this:
$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";
I get this error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.
Line 79 is this line:
while ($row = mysql_fetch_assoc($query)) {
Can anyone tell me how I am not using the ORDER BY correctly, its got me puzzled. :/
Try the query as below
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");
Where you have gone wrong is LIMIT should come after ORDER BY. Read more
Change the query:
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit}") ;
$query = mysql_query("SELECT * FROM {$statement} ORDER BY model DESC LIMIT {$startpoint}, {$limit} ");

select similar value from MySQL and order the result

how do I order this result??
$range = 5; // you'll be selecting around this range.
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want.
$result = mysql_query("select * from table where rank between $min and $max limit $limit");
while($row = mysql_fetch_array($result))
{
echo $row['name']." - ".$row['rank']."<br>";
}
$result = mysql_query(
"select * from table where rank between $min and $max " .
"order by rank asc limit $limit"
);
Use the "order by"- clause:
mysql_query("select * from table where rank between $min and $max order by rank limit $limit");
This will order your result from little to big values.
Use "order by rank desc" to order in descendent direction. (big -> little)

Categories