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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CoolSwoosh
Forum Newbie
Posts: 3
Joined: Fri May 14, 2004 10:15 pm

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

Post 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";
   }
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is the order in reverse? try adding ASC after ExpID in your query string..
CoolSwoosh
Forum Newbie
Posts: 3
Joined: Fri May 14, 2004 10:15 pm

Post 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!
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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 :).
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
CoolSwoosh
Forum Newbie
Posts: 3
Joined: Fri May 14, 2004 10:15 pm

Post by CoolSwoosh »

That was the problem. I really appreciate the help! Thanks again!
Post Reply