Page 1 of 1

Order By - Works in phpMyAdmin, not in PHP Code...

Posted: Fri May 14, 2004 10:15 pm
by CoolSwoosh
Hi guys! i'm new to the board and have a quick question. This is the first time I've ever come across this kind of issue (after developing PHP Applications for 2+ years).

What I'm trying to do is really simple. I'm trying to run a query on my (mysql) database and sort it by the ExpID (INT(11), Primary, auto_inc).

When I run the query in phpMyAdmin, it sorts correctly. BUT, When I run the query through PHP code and output it using mysql_fetch_array(), it doesn't sort correctly. I've used this type of code several times before. Below is a snip of my code, and any help would be GREATLY appreciated.

Code: Select all

<?php

$result = mysql_query("select * from exploded where ProdPartNum = '$PartNum' ORDER BY ExpID");
  if (!$result) echo "Cannot connect to database server-please try again later"; 

$num_rows = mysql_num_rows($result);	
$columns=4;
if($num_rows!=0)
{
	while($row = mysql_fetch_array($result)) 
	{
		 $rows = ceil($num_rows / $columns);
		 $ExpID[] = $row['ExpID'];
		 $ExpKey[] = $row['ExpKey'];
		 $ExpProdPartNum[]=$row['ExpProdPartNum'];
		 $ExpProdDesc[] = $row['ExpProdDesc'];
	}
....table setup....
                 
for($i = 0; $i < $rows; $i++)
{
for($j = 0; $j < $columns; $j++) 
{
 if(isset($ExpProdPartNum[$i + ($j * $rows)])) 
  {
   echo "<TR>\n";
   echo "<TD>" . $ExpID[$i + ($j * $rows)] . "</TD>\n";
   echo "<TD>" . $ExpKey[$i + ($j * $rows)] . "</TD>\n";
   echo "<TD>" . $ExpProdPartNum[$i + ($j * $rows)] . "</TD>\n";
   echo "<TD>" . $ExpProdDesc[$i + ($j * $rows)] . "</TD>\n";
   echo "</TR>\n";
   }
}

?>

Posted: Sat May 15, 2004 12:17 am
by feyd
is the order in reverse? try adding ASC after ExpID in your query string..

Posted: Sat May 15, 2004 7:20 am
by CoolSwoosh
feyd-

Appreciate your response! I already tried adding both ASC/DESC at the end of my query. It does change the order of what is coming out, but it is still incorrect.

I'm thinking the problem is in the PHP code where I setup the arrays...But I've never had such a problem before, so I'm stumped.

Any more ideas? Thanks Again!

Posted: Sat May 15, 2004 9:21 am
by qads

Code: Select all

$result = mysql_query("select * from `exploded` where `ProdPartNum` = '$PartNum' ORDER BY `ExpID`")or die(mysql_error());
give that try, it should tell you why its getting a error message :).

Posted: Sat May 15, 2004 10:51 am
by markl999
I'm not sure why you're doing what you're doing unless the reason isn't in the code you pasted. I can't see why you just arn't doing:

Code: Select all

while($row = mysql_fetch_assoc($result))
{
    echo "<TR>\n";
    echo "<TD>" . $row['ExpID'] . "</TD>\n";
    echo "<TD>" . $row['ExpKey'] . "</TD>\n";
    echo "<TD>" . $row['ExpProdPartNum'] . "</TD>\n";
    echo "<TD>" . $row['ExpProdDesc'] . "</TD>\n";
    echo "</TR>\n";
}
The rows will be coming out in the right order and it is all that $i + ($j * $rows) that's making it display out of order.

Posted: Sat May 15, 2004 11:10 am
by CoolSwoosh
That was the problem. I really appreciate the help! Thanks again!