MySQL query results not displaying - php

I'm trying really hard to figure out why information from a database won't show up. I think my query is failing, but I've talked to a few people and they don't know why it's not working.
Here's my code leading up to the query:
$getnum = mysql_query("SELECT * FROM articles ORDER BY artnum DESC LIMIT 1");
while($getnumrow = mysql_fetch_array($getnum))
{
$theartnum=$getnumrow['artnum'];
}
$pagenum=intval($_GET['pg']);
if($pagenum==0 || !isset($pagenum))
{
$pagenum=1;
}
$offsetnum = $theartnum-($pagenum*15)+15;
echo("SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET $offsetnum");
$result=mysql_query("SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET $offsetnum");
I've went over the variables, and all seem to be working. Echoing the query, I get:
SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET 85
Which should work, since I've checked and the amount in articles is 85.
Much later in the code, I have:
while($row = mysql_fetch_array($result))
{
$art_title=$row['art_title'];
$art_title_url=$row['art_title_url'];
$art_author=$row['art_author'];
$art_date=$row['art_date'];
$artnumber=$row['artnum'];
$desc=implode(' ', array_slice(explode(' ', $row['article']), 0, 14))."...";
echo "<div class=\"big\">".$art_title."</div>
<div class=\"small\">".$art_date." — <a title=\"View more by ".$art_author."\" href=\"author.php?a=".$art_author."\">".$art_author."</a></div>
<span class=\"article\">".$desc."</span><br /><br />";
}
If I put
echo "test";
in there, I don't get that either.
My whole code is here:
http://pastebin.com/RUpb0tUG
(note: It's not complete, I'm still working on the previous/next buttons and I can do that fairly easily)
I'm testing it here until it works.
Thanks!

SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET 85
Which should work, since I've checked and the amount in articles is
85.
Aha, if you have 85 records in your table, what do you expect to get from the above select from row number 86 to 100 other than an empty result set ??

Any chance the problem lies in your "header.php" include file containing code that uses the $result variable or another variable that will screw up your later code?

Related

How to paginate sql query result?

I have some PHP code, which returns all records from my database. I need 10 results on each page. How to paginate the results? Dunno how to write a piece of code responsible for displaying page numbers...
<?php
$query = $link->query("SELECT * FROM news WHERE category='rhythmix' ORDER BY subject DESC");
while($output = $query->fetch_assoc()){
$text = $output['news'];
$text = str_replace('[video]','<div class="video-container">',$text);
$text = str_replace('[/video]','</div>',$text);
$text = str_replace('[media]','<center>',$text);
$text = str_replace('[/media]','</center>',$text);
$embera = new \Embera\Embera();
echo $embera->autoEmbed($text);
}
?>
Add LIMIT to your query in this way:
SELECT *
FROM news
WHERE category='rhythmix'
ORDER BY subject DESC
LIMIT 0, 10;
The first number (0) is the start position on the resulset and the second one (10) is how many results you want to show (the offset).
To show the second page:
...LIMIT 10, 10
If you always want to show 10 results you just need to to add 10 to the first number of the LIMIT.
Change your query to read
$query = $link->query("SELECT * FROM news WHERE category='rhythmix' ORDER BY subject DESC LIMIT 0, 10");
To display the first 10 rows. The next set of 10 rows can be shown using
...LIMIT 10, 10");
and so on. . .

SQL ORDER BY not working

Get the latest 4 donations that come into the system .
Donation IDs are auto incremented (201 , 202, 203 , 204) .. therefore I wanted to order by DESC to get the latest 4 donations.
$resultsdonations = mysql_query("SELECT * FROM donationstable ORDER BY 'donation_id' DESC LIMIT 0,4 ");
Remove the apostrophes around 'donation_id' and the 0 is unnecessary in the LIMIT clause considering your requirement:
$resultsdonations = mysql_query(
"SELECT * FROM donationstable ORDER BY donation_id DESC LIMIT 4");
Try this:
$resultsdonations = mysql_query("SELECT * FROM donationstable ORDER BY donation_id DESC LIMIT 0,4 ");
(drop the ' around the donation_id column)
You don't need the quotes around donation_id. Just use .. BY donation_id DE ..
As the other answers have pointed out, you must remove the quotes from around donation_id.
As it currently stands, you are not sorting by the value in the donation_id column, but rather by the string literal 'donation_id', which is constant for each record and therefore results in an indeterminate ordering.

Select top 50 rows from a table, but display oldest to newest?

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");

How to retrieve all rows from a certain offset up to a certain limit?

Here is my problem, so far I got this script:
$result = mysql_query(
"SELECT *
FROM $tableVideos
ORDER BY VideoTimestamp DESC
LIMIT 6"
) or die(mysql_error());
And that will make it so that I can show the 6 newest uploaded videos to my site.
But how can I have a limit of 6 and start from the 6th newest video and go up to the 12th? In that way I could show 6 videos and make a next button to show the next 6 videos.
You currently have LIMIT 6 to get the first 6 results. If you use
LIMIT a,b
then you will get b items starting from the ath.
You want
LIMIT 6,6
Meaning "Start with the 6th entry, and give me 6 more".
Read more about it.
Use this:
$result = mysql_query("SELECT * FROM $tableVideos ORDER BY VideoTimestamp DESC LIMIT 6,6") or die(mysql_error());
Actually Limit function have two paramter :
one is limit of record and second is start from where.
result = mysql_query("SELECT * FROM $tableVideos ORDER
BY VideoTimestamp DESC LIMIT 6,6") or die(mysql_error());

Ranking/numbering in php

I tried to achieve a method in which I create a scoreboard (while loop) and order the fetched results by a certain numeric field (points). But what I need to achieve is like the following
rank----username--point
1st------test-----------3200
2nd-----test2---------1200
etc.. I paginate my results and limit 25 results per page. I tried a while loop in the following way
while($row = mysql_fetch_array($query) || $i<=$totalnumberofrows ){
echo out the results
$i++
}
What this does is number from 1 to 25 perfectly, but on the other hand, on page 2 it numbers again from 1-25.
Is there a trick to achieve what I need to achieve?
I.e. continuous numbering from 1 to (total number of rows) even when paginated.
What you're looking for is the MySQL LIMIT statement. For example, the following query would return entry 0-24 (so, the first 25 entries in your database):
SELECT * FROM entries ORDER BY `points` LIMIT 0, 25
Say that you want to fetch the second page (the next 25 entries), you can do:
SELECT * FROM entries ORDER BY `points` LIMIT 25, 25
Most important thing to note here is that the first argument is the row where it should start, and the second argument is not the last row, but the total amount of rows it should return.
To determine the starting point, simply do (($page_number - 1) * 25), assuming your first page is numbered 1. You could for example do the following:
<?php
$start = (($page_number - 1) * 25);
$query = "SELECT * FROM entries ORDER BY `points` LIMIT {$start}, 25";
// ... rest of your code goes here ...
?>
just add 25 to the number for each page,
for the second page (in my example) - add 25
for the third page - add 50
etc...
$page = 2;
while($row = mysql_fetch_array($query) || $i<=$totalnumberofrows ){
echo out the results
echo (($page-1)*$totalnumberofrows)+$i;
$i++
}
If you know the page number and the offset you should be able to do the following
$rank = $offset * $page_number;

Categories