PDA

View Full Version : ORDER by id


van21691
05-26-2009, 08:15 AM
I just don't get it, I put id as my primary, I have my code to DESC. It still not doing its job.


<?php
// Make a MySQL Connection
mysql_connect("localhost", "xxxxx", "1234") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());

$result = mysql_query("SELECT * FROM table ORDER BY id DESC " )
or die(mysql_error());

$max = 10; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
//view the news article!
if(isset($_GET['name']) && $_GET['fsid'] == "view")
{
$fsid = $_GET['fsid'];
$sql = mysql_query("SELECT * FROM kc WHERE fsid = '$fsid'");
while($r = mysql_fetch_array($sql))
{

$name = $r['name'];
$fsid = $r['fsid'];
$id = $r['id'];
$email = $r['email'];
$image = $r['image'];
$quote = $r['quote'];
echo " <br>";
}
}else{

//view all the news articles in rows
$sql = mysql_query("SELECT * FROM kc LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM kc"),0);
$totalpages = ceil($totalres / $max);
//the table
echo "<br>";
while($r = mysql_fetch_array($sql))

{
$id = $r['id'];
$fsid = $r['fsid'];
$name = $r['name'];
$email = $r['email'];
$time = $r['time'];
$image = $r['image'];
$quote = $r['quote'];


echo "<table width=360 height=190 border=1><tr>";
echo "<th width=145 rowspan=6 scope=row><img src=";
echo $r['image'];
echo " width=100 height=120></th><td colspan=2><div align=center>";
echo " -";
echo $r['id'];
echo "-";
echo "</div></td></tr><tr><td width=83>";
echo "<div align=right>NAME</div></td><td width=136><div align=left>";
echo $r['name'];
echo "</div></td></tr> <tr>";
echo "<td><div align=right>Email Add</div></td><td><div align=left>";
echo $r['email'];
echo "</div></td></tr><tr>";
echo "<td><div align=right>Quote</div></td> <td><div align=left>";
echo $r['quote'];
echo "</div></td></tr> <tr>";
echo "<td colspan=2><div align=center><a href=http://profiles.friendster.com/";
echo $r['fsid'];
echo ">View my profile</a></div></td></tr></table>";

}
}
//close up the table
echo "<br>";
for($i = 1; $i <= $totalpages; $i++){
//this is the pagination link
echo "<a href='index.php?p=$i'>$i</a> | ";
}

?>



This is the form
<FORM action="/chat/insert.php" method="post">
<font color=black>Name:</font> </font><input type="text" name="name" onClick="notEmpty(document.getElementById('req1'), 'Please Enter a Value')"><br>
<font color=black>Email:</font> <input type="text" name="email" onClick="notEmpty(document.getElementById('req2'), 'Please Enter a Value')"><br>
<font color=black>Image:</font><input type="text" name="image" value="http://thiz.ismywebsite.com/chat/nophoto.jpg"> <font color=black>- Include <b>HTTP://</b></b><br>
<font color=black>Friendster ID:</font><input type="text" name="fsid" onClick="notEmpty(document.getElementById('req3'), 'Please Enter a Value')"> - <br>
<font color=black>Quote:</font> <input type="text" name="quote" onClick="notEmpty(document.getElementById('req4'), 'Please Enter a Value')">
<INPUT type="submit" value="submit">
Can someone check if the code is right, I'm still learing php and mysql

guelphdad
05-26-2009, 04:47 PM
Two things to consider

1) when coding, separate your sql from your code. test the sql directly in the database and make sure it is returning correct results. When you know that is the case then add the query to your front end code (in this case PHP).

2) when you run the query

SELECT * FROM table ORDER BY id DESC


can you show the output it is producing for the id column so we don't have to guess what is wrong? Though I will take a stab at it and say you are using a varchar/char type for the id column and your output is like this:

1
10
100
2
3
30
3000
4

etc. (or since you specified DESC the reverse of the above).

Fumigator
05-26-2009, 04:49 PM
What's the problem, specifically?

van21691
05-27-2009, 01:48 AM
the result is not in DESC order instead it is in ASC order.
I have an id which increase everytime a user press submit, their info will be submitted to the database and it will appear on the table.

the result shows:
1
2
3
4
5...

But I want it to be the latest posts to the first one

...100
99
98
99 until
1

Old Pedant
05-27-2009, 02:26 AM
Ummm...Not to ask a dumbass question...

But since you NEVER USE the $result variable--which is indeed the result of that ORDER BY ID DESC query--how do you even *KNOW* what order you are getting the records in?????????

You do this:
$result = mysql_query("SELECT * FROM table ORDER BY id DESC " )
or die(mysql_error());

and then NEVER look at $result, ever again, in ANY of that code.

There is some other really really odd junk in there, too.

For example, you code this:

if(isset($_GET['name']) && $_GET['fsid'] == "view")
{
$fsid = $_GET['fsid'];
$sql = mysql_query("SELECT * FROM kc WHERE fsid = '$fsid'");
...

Ummm...how do you even *GET* to the lines in magenta if $fsid is ANYTHING other than view???

And why do you care what the value (or non value) of $_GET['name'] is when, again, you NEVER EVER use that value???

So why didn't you just code that as

if( $_GET['fsid'] == "view" )
{
$sql = mysql_query("SELECT * FROM kc WHERE fsid = 'view'");
...


Sorry, but I think this code has more problems than you are looking at, as yet.

van21691
05-27-2009, 04:39 AM
I learn from mistakes. Sorry.
I'll try and fix this