Dispaly range of values from ID table in MySQL - php

I want display the date range for my ID like from ID 4 to 6 (means not using limit) but as shown in the picture All value
In my code all the values store in category table is displaying I just want to display only first three records.
my code is here
$sql = "SELECT * FROM category order by id ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '
<span id="rcorners2">'.$row["category"].' </span>

In mysql you can use limit 3
SELECT * FROM category order by id ASC LIMIT 3

Related

I need to reverse the order of my ORDER BY results

I need to get 3 rows with the lowest value in a certain column, and then reverse the order of these 3 rows. So if the 3 rows with the lowest value are A, B and C, I need to sort them as C, B and A. Could I do that in a single SQL statement?
$sql = "SELECT * FROM ratings_table ORDER BY rating DESC LIMIT 3";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<td>".$row['rating']."</td>";
}
}
With this code I will get the right results but in the wrong order.
You'll want to use a subquery, like this:
SELECT *
FROM
(
SELECT *
FROM ratings_table
ORDER BY rating ASC LIMIT 3
) a
ORDER BY rating DESC
This will take the lowest three results , then flop the order.
Only a sub-query will work. First you select the 3 lowest values and then sort them.
Something like:
SELECT * FROM (
SELECT <COLUMN_VALUE>
FROM <YOUR_TABLE>
ORDER BY <COLUMN_VALUE> LIMIT 3
) T
ORDER BY <COLUMN_VALUE> DESC
$sql = "SELECT * FROM ratings_table ORDER BY rating DESC LIMIT 3";
$result = mysqli_query($conn, $sql)->fetch_all();
krsort($result)
foreach ($result as $row)
{
echo "<td>".$row['rating']."</td>";
}

how to convert multiple row data into array and subtract it from another array?

I am fetching multiple rows from my data base like,
$query = "SELECT * FROM student_info where district = '".$admdistrict."' AND user_status = 'approved' ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());
$delete = mysql_fetch_array($resultt);
$std_delete_array = explode(',', $delete['id']);
the value of id in database is like 4,5,6 in different rows but it is giving only first value 4.
Another array I am fetching is,
$query="SELECT * FROM events where id='$district'";
$showdata=mysql_query($query) or die(mysql_error());
$user=mysql_fetch_array($showdata);
$std_fulllist_array = explode(',', $user['std_list']);
the value in database is 4,5,6,8. But in single coloumn, so it is giving proper output. now I want to subtract $std_delete_array from $std_fulllist_array.
I am using following code,
$values = array_diff($std_fulllist_array, $std_delete_array);
which is only subtracting first value of $std_delete array.
strucutre of table student_info is
id ! name ! District
4 a panipat
5 b panipat
6 c panipat
strucutre of table events is
id ! district ! std_list
1 panipat 4,5,6
2 karnal 4,7,8
3 chandigarh 5,6,7
You are saying the value of id in database is like 4,5,6 in different rows
that means your table is look like
id district
-- ---------
4 abc
5 def
6 geh
8 ijk
so your first query is fetching multiple records.
EDIT
So your query would be
$ret_ = array();
$query = "SELECT * FROM student_info where district = '".$admdistrict."' AND user_status = 'approved' ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_array($result)) {
echo $row['id'];
$ret_[] = $row;
}
}
print_r($ret_);

SQL LIKE returns all records

When I run the following query in Phpmyadmin it returns 10 records, like it should
SELECT * FROM sku WHERE sku LIKE '142-401-117-282%'
But when I run the same query in my Php script, I get all the rows from the table
$sql = "SELECT * FROM sku WHERE sku LIKE '142-401-117-282%' ";
$result = $con->query($sql);
while($row = mysqli_fetch_array($result)){
echo $row['sku']."<br>";
}
I'm selecting all the SKU's that start with 142-401-117-282, after come a - plus the size of the item
How do I get the 10 rows that I need?
Try:
$sql = "SELECT * FROM sku WHERE sku LIKE '142-401-117-282%' LIMIT 0,10";
where 0 is starting row position and 10 is its length.

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'];
}

Selecting the most popular entry from the last ten values entered

I have the selecting from the last ten entries working, but am unsure how to get the most popular from these ten entries? Also how would I count the number of the most popular entry & output it to a percentage?
<?php
$sql = "SELECT data FROM table_answers ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo "[".$row['data']."]";
}
?>
And I have tried to do the WHERE value as well but it doesn't return any result.
$sql = "SELECT data FROM table_answers WHERE id IN (SELECT id FROM table_answers
ORDER BY id DESC LIMIT 10) ORDER BY popularity DESC LIMIT 1";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo " [".$row['data']."] ";
}
Anyone have any idea what I might be doing wrong here? please
This should solve the problem -
SELECT tableorder.*
FROM (SELECT *
FROM table
ORDER BY id DESC
LIMIT 10) tableorder
ORDER BY tableorder.popularity DESC
LIMIT 1
The inner query will sort on the basis on id and get the top 10. The outer will again sort the 10 rows on the basis of popularity and return the row with highest popularity.
SELECT data
FROM (
SELECT data
FROM table_answers
ORDER BY id DESC
LIMIT 10
) t
ORDER BY popularity

Categories