[Help!!][PHP]Newssystem DB read

Joined
Jan 5, 2007
Messages
652
Reaction score
7
Location
Megaton
i want to make this script to read from the last id in the database to
id 0 (the first one in the database)last number changes all the
time when somone add's new news..

can somone plz tell me how to do it

im trying to do it for 2 days now
(yes im a noob at php stil learning
i found this script at the internet)


shadow-xx - [Help!!][PHP]Newssystem DB read - RaGEZONE Forums



Code:
<?php
 include('config.php');
  
  mysql_connect($host, $user, $pass);
  mysql_select_db($db);
  
  $sql = "SELECT * FROM news";
  $result = mysql_query($sql);
  $num = mysql_num_rows($result);
  
  while($row = mysql_fetch_array($result)){
    echo "<font size=4><b>".$row['title']."</b></font><br><font size=1>".$row['writer']."</font>";
  echo "<font size=-2> - ".$row['posted_at']."</font><br><br>";
  echo "<font size=-1>";
  include($row['text_url']);
  echo "</font>";
  }
  
  mysql_close();
?>
 
i got this (look script)
it reads from oldest to newest
the oldest message is at the top i
want it to put the newest at the top

but i need somone to tell me how
im newb at php just started
i only know the simple things like
how to show the ip of somone
or how to read fields from a form


the script online: (the errors ae the missing text files)
http://shadow-xx.110mb.com/news/news.php
 
The current errors I saw on your site was not being able to connect to MySQL database.

As for your question, you just need to add ORDER BY id DESC

Replace:
Code:
$sql = "SELECT * FROM news";
To:
Code:
$sql = "SELECT * FROM news ORDER BY id DESC";
 
the error are some links to text files in the database (text files dont exist)

thnx
but u mean it works or i have to change order by id
in to somthing like (last_id,', ',first_id)
 
sage

...
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1
To sort in reverse order, add the DESC (descending) keyword to the name of the column in the ORDER BY clause that you are sorting by.
Read. Repeat until understood.
 
the error are some links to text files in the database (text files dont exist)

thnx
but u mean it works or i have to change order by id
in to somthing like (last_id,', ',first_id)

Ok i try to explain in further details, take a look at your original query.
Code:
$sql = "SELECT * FROM news";
This query only select everything from your `news` table.

Now furthermore, if you want to arrange them in order (desc or acs) you will need to put extra query, take a look
Code:
$sql = "SELECT * FROM news ORDER BY id DESC";
This query is about the same with the first query, but it add ORDER BY id DESC which means it will order by the id in desc..
Example 10,9,8,7,6...3,2,1,0

Hope this clear your doubt.
 
or just try this (hope it helps)

<?php
include('config.php');

mysql_connect($host, $user, $pass);
mysql_select_db($db);

$sql = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC");
$num = mysql_num_rows($sql);

while($row = mysql_fetch_array($sql)){
echo "<font size=4><b>".$row['title']."</b></font><br><font size=1>".$row['writer']."</font>";
echo "<font size=-2> - ".$row['posted_at']."</font><br><br>";
echo "<font size=-1>";
include($row['text_url']);
echo "</font>";
}

mysql_close();
?>
and it looks like ur not usint the $num variable =S
 
Re: sage

...

Don't spoonfeed the noobs -.-

They will not learn anything.

My second quote is all that the OP needs.

Nah.. We aren't spoonfeeding him, I even write down in details on the query that would actually helps him understand. And do you expect them to understand from there? Whereas they're just a beginner, think twice man.

P.S: Don't call people noobs, they are just starting out.
 
Back