Can someone advise on how to get around this problem?
I want to call the top 50 rows by id from my SHOUTBOX table, but display them in a div in ascending order (the most recent row (newest) at the bottom)
for example;
most recent row is id 200,
I want to call rows 150 - 200,
display them in the div like;
150,
151,
152,
.
.
.etc
.
.
200 <<< last line in DIV
my PHP code currently looks like this;
$recall=mysql_query("SELECT *, DATE_FORMAT(timepost,'%H:%i:%s') as timepost FROM shoutbox ORDER BY id DESC LIMIT 50");
while($comm=mysql_fetch_object($recall)){
if ($comm->poster == "System"){
print"<font color=#3399FF>$comm->timepost-<strong><a href='profile.php?viewuser=$comm->poster' target='iframecontent'><font color=#3399FF>$comm->poster</font></a></strong>: </font>";
echo replace($comm->post);
echo"<br>";
}
but it returns my data in the div like;
200,
199,
198,
.
.
.etc
.
.
150 <<< last line in DIV
Please can anyone help?
note: I am currently working through all my pages and converting all mySQL queries to PDO
For future reference, if you're converting to PDO there is the exact answer for reading backwards on this page without changing your query. http://www.php.net/manual/en/pdostatement.fetch.php.
For now this will work:
SELECT *, DATE_FORMAT(timepost,'%H:%i:%s') AS timepost FROM (SELECT * FROM shoutbox ORDER BY id DESC LIMIT 50) AS foo ORDER BY id ASC
Ok, try this:
$recall=mysql_query("SELECT *, DATE_FORMAT(timepost,'%H:%i:%s') as timepost FROM (SELECT * FROM shoutbox ORDER BY id DESC LIMIT 50) ORDER BY id ASC");
replace ORDER BY id DESC by ORDER BY id
You have wrong the order command, you have to put ASC instead of DESC Here is your correct query:
$recall=mysql_query("SELECT *, DATE_FORMAT(timepost,'%H:%i:%s') as timepost FROM shoutbox ORDER BY id ASC LIMIT 50");
while($comm=mysql_fetch_object($recall)){
if ($comm->poster == "System"){
print"<font color=#3399FF>$comm->timepost-<strong><a href='profile.php?viewuser=$comm->poster' target='iframecontent'><font color=#3399FF>$comm->poster</font></a></strong>: </font>";
echo replace($comm->post);
echo"<br>";
}
Change your query to:
$recall = mysql_query("SELECT *, DATE_FORMAT(timepost,'%H:%i:%s') as timepost
FROM shoutbox ORDER BY id ASC LIMIT 50");
Related
I had a SELECT query like this at first..
($con,"SELECT * FROM users ORDER BY id ASC LIMIT 1");
But, I realized that I needed the newest user out of the groups 2, 3, 4, or 5 (excluding group 1). So in the end I only want one user showing that is the newest.
What is wrong with my SELECT query now that it would not show up anything?
//Newest Member
$member = mysqli_query($con,"SELECT * FROM users WHERE `group`=2, 3, 4,5 ORDER BY id ASC LIMIT 1");
$numrows_member = mysqli_num_rows($member);
if($numrows_member > 0){
while($row_member = mysqli_fetch_assoc($member)){
$memberid = $row_member['id'];
$member_username = $row_member['username'];
echo $member_username;
}
} else {
echo "No Members Found...";
}
You are looking for the IN operator. The syntax would be like this:
SELECT *
FROM users
WHERE `group` IN (2, 3, 4, 5)
ORDER BY id ASC
LIMIT 1;
There is a reference here.
Note: Based on your comment, if you are looking for the latest user you may want to order in DESC order instead, so that the largest id is put first. Even better (in my opinion), if you store the account_created_date you could order by that. Again, in descending order.
If you want to exclude a specific group, use != instead of trying to find one of the remaining groups:
SELECT * FROM users WHERE `group` != 1 ORDER BY id ASC LIMIT 1
I have written a MySql query to get the columns related with minimum id . Looks something like this
SELECT min(id) as ID,feed , idpropiedad FROM `registrofeed` WHERE feed=21
The table has 4 rows looks like this
So according to the function that I have written
function setLC()
{
$sql = "
SELECT min(id) as ID
, feed
, idpropiedad
FROM `registrofeed`
WHERE feed=21
";
$result = $this->localDb->execute($sql);
$row=mysql_fetch_array($result);
echo $sql;
echo $row['idpropiedad'];
$this->lastCode = $row['idpropiedad'];
}
It returns empty string for idpropiedad
Can any one help me out where I am going wrong
Thanks in advance
I'd think the query you're actually looking for is this:
SELECT id, feed, idpropiedad
FROM registrofeed
WHERE feed = 21
ORDER BY id ASC
LIMIT 1
MIN() is giving you the generally lowest value in the column, it does not affect the rest of the columns. If you want the whole row with the lowest id it doesn't help.
To illustrate, if you really wanted to use MIN here, you'd have to do:
SELECT id, feed, idpropiedad
FROM registrofeed
WHERE id = (SELECT MIN(id) FROM registrofeed WHERE feed = 21)
You can do a better query like this:
$sql = "
SELECT id as ID
, feed
, idpropiedad
FROM `registrofeed`
WHERE feed=21
HAVING MIN(id)
";
This will return only one row with the minimum id number. It's more readable than using ORDERING AND LIMIT 1.
try your select query as
SELECT * FROM registrofeed WHERE feed='21' ORDER BY id ASC LIMIT 1
this fetches the row having minimum id.
Hope it helps
Try this
$sql = "SELECT min(id) as ID,feed , idpropiedad FROM `registrofeed` WHERE feed='21' order by id asc";
So I'm trying to add a little bit of convenience to a CRUD by adding next and previous links to navigate between records in my database.
Here are my queries:
$id=$_GET['id'];
$id = $currentid;
$prevquery= "SELECT * FROM inventory WHERE id < $currentid ORDER BY id DESC LIMIT 1";
$prevresult = mysql_query($prevquery);
$nextquery= "SELECT * FROM inventory WHERE id > $currentid ORDER BY id ASC LIMIT 1";
$nextresult = mysql_query($nextquery);
?>
Here is my HTML:
Previous
Next
Now I tested these queries in PHPMyAdmin and they produced the result I wanted, but I can't get my hyperlinks to actually be supplied with the correct IDs... they're just blank after the =. What am I doing wrong?
mysql_query() returns a result set (resource). To get the actual rows from the result set, you need to use a function like mysql_fetch_row().
Your code for the "next" link would look something like:
PHP
$nextquery= "SELECT * FROM inventory WHERE id > $currentid ORDER BY id ASC LIMIT 1";
$nextresult = mysql_query($nextquery);
if(mysql_num_rows($nextresult) > 0)
{
$nextrow = mysql_fetch_row($nextresult);
$nextid = $nextrow['id'];
}
HTML
Next
and the previous link would be done similarly.
Obligatory note: For new code, you should seriously consider using PDO.
Advanced note:
You could combine your queries into a single query like:
SELECT
(
SELECT id
FROM inventory WHERE id < $currentid ORDER BY id DESC LIMIT 1
) AS previd,
(
SELECT id
FROM inventory WHERE id > $currentid ORDER BY id ASC LIMIT 1
) AS nextid
And then adjust the logic accordingly.
Okay, It took a little bit but I figured it out...
PHP
$prevquery= "SELECT * FROM inventory WHERE id < $currentid ORDER BY id DESC LIMIT 1";
$prevresult = mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_array($prevresult))
{
$previd = $prevrow['id'];
}
$nextquery= "SELECT * FROM inventory WHERE id > $currentid ORDER BY id ASC LIMIT 1";
$nextresult = mysql_query($nextquery) or die(mysql_error());
while($nextrow = mysql_fetch_array($nextresult))
{
$nextid = $nextrow['id'];
}
HTML
Previous
Next
Thanks for the help, I think it put me on the right course. I'll look into that PDO stuff for the future. I'm just now starting to get a hang of the old MYSQL/PHP syntax and now all the sudden there's a new format... tough to keep up with it all!
The resultant code has a culprit at the very beginning and end of the table. Your code will "die" as you ordered. Instead you might check the query results ($prevresult, $nextresult) and decide NOT to show a link for previous or next item.
SELECT
IFNULL(
(SELECT id FROM inventory WHERE id < $currentid ORDER BY id DESC LIMIT 1 ) , (SELECT id FROM inventory ORDER BY id DESC LIMIT 1 )
) AS previd ,
IFNULL(
(SELECT id FROM inventory WHERE id > $currentid ORDER BY id ASC LIMIT 1 ),
(SELECT id FROM inventory ORDER BY id ASC LIMIT 1 )
) AS nextid
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
I want to get all rows count in my sql.
Table's first 2 columns look like that
My function looks like that
$limit=2;
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
$stmt = $this->db->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $total, $datetime, $title, $content);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count > 0) {
while ($stmt->fetch()) {
Inside loop, I'm getting exact value of $total, but MySQL selects only 1 row - row with id number 1. (and $count is 1 too)
Tried this sql
SELECT id,dt,title,content FROM news ORDER BY dt DESC LIMIT 2
All goes well.
Why in first case it selects only 1 row? How can I fix this issue?
for ex my table has 5 rows. I want to get 2 of them with all fields, and get all rows count (5 in this case) by one query.
Remove COUNT(*). You will only ever get 1 row if you leave it in there.
Try adding GROUP BY dt if you want to use COUNT(*) (not sure why you're using it though).
EDIT
Fine, if you insist on doing it in a single call, here:
$sql = "SELECT id,(SELECT COUNT(id) FROM news) as total,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
This is likely cause by the variable $limit being set to 1, or not being set and mysql defaulting to 1. Try changing your first line to
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC";
EDIT
Change to:
$sql = "SELECT SQL_CALC_FOUND_ROWS,id,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
And then use a second query with
SELECT FOUND_ROWS( )
to get the number of rows that match the query
This totally wreaks of a HW problem... why else besides a professor's retarded method to add complexity to a simple problem would you not want to run two queries?
anyways.... here:
SELECT id, (SELECT COUNT(*) FROM news) AS row_count, dt, title, content FROM news ORDER BY dt DESC LIMIT