Limit query results after receiving result - php

I'm having sort of an issue with sorting and giving sort of a ranking while having pagination.
$query = "SELECT * FROM users WHERE approved=1 ORDER BY Likes DESC LIMIT $start_from, $num_rec_per_page";
$result = mysqli_query($conn,$query);
$i=1;
while($row = mysqli_fetch_assoc($result)){
echo 'Rank: '.$i++.'. Name:'.$row[Name].'Likes: '.$row[Likes].'<br>';
}
This way, every page rank is given all over again.
My question is, how to Limit query only AFTER receiving all data,
or maybe some advice on doing other way of pagionation.
UPDATE:
Fixed the problem by changing $i=1; to $i=$start_from+1;

Limiting the $i ?
$maxPerPage = 20;
$i = 0;
while($i < $maxPerPage && $row = mysqli_fetch_assoc($result)){
echo 'Rank: '.$i++.'. Name:'.$row[Name].'Likes: '.$row[Likes].'<br>';
}
But LIMITING in mysql (in your case) is better than getting all rows and fetching just some.

Related

Select last row mysqli

$select1 = mysqli_query($conn, "SELECT robotid FROM robotidsz ORDER BY robotid DESC LIMIT 1");
for ($i = $select1; $i <= 9999999; $i++) {
Above is my code, this should search from the last "RobotID known in the table to 9999999.
If i change $select1 to 1000000 this functions from 1000000-9999999 correctly however using this code nothing happens?
You need to extract the result from your query. At the moment, you're assigning $i an initial value of the Resource returned.
The following demonstrates how to solve this:
$select1 = mysqli_query($conn, "SELECT robotid FROM robotidsz ORDER BY robotid DESC LIMIT 1");
$result = mysqli_fetch_object($select1);
for ($i = $result->robotid; $i <= 9999999; $i++)
{
// Do your stuff...
}

How to select the maximum value of a column in MySQL

I want to select the maximum value of a column of my table. I'm using PHP and MySQL. This is what I have so far:
$max = "SELECT MAX(Id) FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
echo= $max1;
My debugger is just saying that it is a query returning a 0 boolean value (false). I cannot find a specific answer anywhere on the internet.
You need to fetch the data from the mysqli_result object that was returned to you when you executed your query using mysqli_query.
$max = "SELECT MAX(Id) as id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1); // this was missing
$id=$row['id'];
echo $id;
Note: I removed the loop because with MAX query without any grouping you will get only 1 row returned. If you had multiple rows in result set you would need to loop through all of them
two ways
first is as people described
$max = "SELECT MAX(Id) as max_id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
$max_id=$row['max_id'];
echo $max_id;
second is ordering and limiting
$max_id = 0;
$max = "SELECT id FROM trialtabel2 order by id desc limit 0,1";
$max1 = mysqli_query($dblink, $max);
while($row = mysqli_fetch_assoc($max1)){
$max_id=$row['id'];
}
echo $max_id;
In Your code you missing the fetch statement. you need to fetch from the resultset. see above what you are missing.
Try using this..,
$max = "SELECT MAX(Id) as maxId FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
echo $row['maxId'];
Hope this helps..
$max = "SELECT Id FROM trialtabel2 order by id desc limit 1";
$max1 = mysqli_query($dblink, $max);
$result = mysqli_fetch_assoc($max1);
pre($result);

PHP Order by ASC not working on decimal type

Have a quick question regarding ORDER. I have a list of scores I am displaying in a high scores table using php. I need the lowest decimal number to display in 1st place, however when I try to use the ASC command, no results display. But if I use DESC the results do display, but in the opposite order to what I need (lowest decimal displays last).
Here is the "working" code that displays the scores, but in the wrong order.
$query = mysql_query("select reflex,playerID from users_stats order by reflex DESC limit 10")or die(mysql_error());
$ranking = 0;
while ($row = mysql_fetch_array($query)) {
if ($row[reflex] <= 0) break;
$ranking = $ranking + 1;
$rankingdisplay = doRankPosition($ranking);
print "<tr><td><b>$rankingdisplay</b></td><td>$row[playerID]</td><td>$row[reflex]</td></tr>";
if ($ranking >= 10) break;
}
Here is the code that displays nothing (no results are returned).
$query = mysql_query("select reflex,playerID from users_stats order by reflex ASC limit 10")or die(mysql_error());
$ranking = 0;
while ($row = mysql_fetch_array($query)) {
if ($row[reflex] <= 0) break;
$ranking = $ranking + 1;
$rankingdisplay = doRankPosition($ranking);
print "<tr><td><b>$rankingdisplay</b></td><td>$row[playerID]</td><td>$row[reflex]</td></tr>";
if ($ranking >= 10) break;
}
I have also tried this (default);
$query = mysql_query("select reflex,playerID from users_stats limit 10")or die(mysql_error());
$ranking = 0;
while ($row = mysql_fetch_array($query)) {
if ($row[reflex] <= 0) break;
$ranking = $ranking + 1;
$rankingdisplay = doRankPosition($ranking);
print "<tr><td><b>$rankingdisplay</b></td><td>$row[playerID]</td><td>$row[reflex]</td></tr>";
if ($ranking >= 10) break;
}
And again, no results display....
The reflex score is being stored in a MySQL database using decimal(4,3) default None. Can anyone point me in the right direction? I have tried to google it, but can't seem to find anything specific to what I need. I assume it is something to do with decimal??
Thanks in advance.
*EDITED - I do appreciate any answers/advice, however I am very new to php and still desperately trying to learn :/
Probably you have 0 in column "reflex" and line
if ($row[reflex] <= 0) break;
made exit from cycle

Echo the 2 last columns of tabel

I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);

MySQL displaying 1 result within while loop...however theirs actually 2 results?

I recently combined 2 queries into 1 (to optimize performance)...1 query for checking the count, and the other for the results, the count is to ensure their is actual results their before proceeding with the loop.
Heres the PHP code:
<?php
$query = 'SELECT id, title, COUNT(id) FROM submissions ORDER BY sub_time DESC LIMIT 50';
$result = mysql_query($query);
$count = mysql_result($result, 0, 2);
mysql_data_seek($result, 0); //mysql_result resets the internal pointer...
if ($count > 0) {
$i = 0;
while ($output = mysql_fetch_assoc($result)) {
++$i;
//etc..
}
}
?>
The problem is it now returns 1 result? - when theirs actually 2 results inside (as I've checked via PHPMyAdmin aswell as running the following code - see below), I've narrowed down the problem, its because the COUNT(id) has been combined with the intial results query; that its behaving like this.
Because if I do the following:
<?php
$query = 'SELECT id, title FROM submissions ORDER BY sub_time DESC LIMIT 50';
$result = mysql_query($query);
$i = 0;
while ($output = mysql_fetch_assoc($result)) {
++$i;
//etc..
}
?>
It returns 2 results...
So my question is how do i resolve this but achieve what I'm after?
I would recommend that you remove the COUNT(id) and use the mysql_num_rows() function to check how many rows were returned before trying to loop through them.
Your code could then look like this:
<?php
$query = 'SELECT id, title FROM submissions ORDER BY sub_time DESC LIMIT 50';
$result = mysql_query($query);
$count = mysql_num_rows($result);
if ($count > 0) {
$i = 0;
while ($output = mysql_fetch_assoc($result)) {
++$i;
//etc..
}
}
COUNT is an aggregate function that typically is used with the GROUP BY clause. I don't believe it is meaningful in the query as you used it.

Categories