SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
IBM developerWorks
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old June 13th, 2002, 10:02 PM
taiyoken taiyoken is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 taiyoken User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to taiyoken
problem retreving information from mySQL database

Hi, my name's Herb and I've been coding in php for about two months now.
I've been working on a news script that uses mysql to store the actual news and user informaion. But I'm having a problem with displaying the actual news from the database as i want it to be displayed. I was using the following code to do so:
php Code:
Original - php Code
  1. $linkID = @mysql_connect("localhost", "", "");
  2. mysql_select_db("news", $linkID);
  3. $resultID = mysql_query("SELECT date, subject, news, status, email, username FROM updates", $linkID);
  4. for($i = 0; $i < 5; $i++)
  5. {
  6.     list($date, $subject, $news, $status, $email, $username) = mysql_fetch_row($resultID)
  7.     print "<table width="340" border="0" cellspacing="2" cellpadding="2" align="center">";
  8.     print "<td width="50" height="50"><img src="/avatars/" . $username . ".gif" width="50" height="50" border="0" alt="://otaku online: " . $status . ": " . $username .""></td>";
  9.     print "<td height="50"><b><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2">" . stripslashes($subject) . "</font></b></td>"
  10.     print "<tr align="left" valign="top">";
  11.     print "<td colspan="2" align="left" valign="top"><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="3">..................................</font></td>";
  12.     print "</tr>";
  13.     print "<tr align="left" valign="top">";
  14.     print "<td colspan="2" align="left" valign="top"><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="1">" . stripslashes($news) . "<br><br>" . $date . " -(<a href="mailto:" . $email . "">" . $username . "</a>)</font></td>";
  15.     print "</tr>";
  16.     print "</table><br>";
  17. }
  18. mysql_close($linkID);

The code displays the first 5 enteries made to the database. I wanted it to display the 5 most recent entries, starting with the most recent entry.
Now i've tried using mysql_num_rows($resultID) as the variable $i and then using another variable to equal 5 less than that using an expression similar to:
php Code:
Original - php Code
  1.  
  2. $a = mysql_num_rows($resultID);
  3. $a -= 5
  4. for($i = mysql_num_rows($resutltID); $a < $i; $a++)

but i would either end up with the same results.

So, does anyone have any ideas on how i could get this script to display the 5 most recent posts?

Reply With Quote
  #2  
Old June 13th, 2002, 10:15 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: problem retreving information from mySQL database

outside of MySQL (since i have yet to learn it) you could give each news entry an ID timestamp of the date it was created, then read through each ID while storing them into an array and then sort the array from lowest to highest (the highest being the most recent update) and then reverse the array to display the top 5 :-D

Reply With Quote
  #3  
Old June 14th, 2002, 02:10 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: problem retreving information from mySQL database

use this for your query :

SELECT date, subject, news, status, email, username FROM updates ORDER BY date DESC LIMIT 5


Reply With Quote
  #4  
Old June 14th, 2002, 02:54 AM
taiyoken taiyoken is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 taiyoken User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to taiyoken
RE: RE: problem retreving information from mySQL database


Quote:
use this for your query :

SELECT date, subject, news, status, email, username FROM updates ORDER BY date DESC LIMIT 5


thank you Anonymous user! With a little bit of tweaking, my script worked perfectly! can you beleve i've spent all day trying to fix that one problem? LOL

Reply With Quote
  #5  
Old June 22nd, 2002, 06:34 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: problem retreving information from mySQL database

In the tutorial "Create dynamic sites with PHP & MySQL", how does one reverse the order of listing. The way there are listed now are be "id":

$result = mysql_query("SELECT * FROM personnel",$db);
echo "<TABLE BORDER=2>";
echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<TR><TD>".$myrow["firstname"]." ".$myrow["lastname"]."<TD>".$myrow["nick"];
echo "<TD><a href="view.php?id=".$myrow[id]."">View</a> ";
echo "<a href="delete.php?id=".$myrow[id]."">Delete</a> ";
echo "<a href="addedit.php?id=".$myrow[id]."">Edit</ a>";
}
echo "</TABLE>";
?>
</HTML>

Hellp?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > problem retreving information from mySQL database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway