Jump to content


Photo

Not working, but not getting an error


  • Please log in to reply
24 replies to this topic

#1 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 01:47 PM

I'm working with a long list of phone extensions, among other things. The page can be viewed [a href=\"http://artelcommunications.net/test/view.php\" target=\"_blank\"]here[/a]. It's fully functional except for the "sort" feature (when you click a column title). It was working two hours ago, but now isn't. I tried echoing my SQL query then pasting it into PHPMyAdmin and I still get no errors. The relevant code is below:

<?
if (!$sort){
$sort = "ASC";
}

if ($sort == "ASC"){
$sort2 = "DESC";
}

if ($sort == "DESC"){
$sort2 = "ASC";
}

$username="admin";
$password="pw";
$database="db";
$heh="site.site.net";

mysql_connect($heh,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if ($orderby){
$result = mysql_query("SELECT * FROM extensions ORDER BY '$orderby'");
}

if (!$orderby){
$orderby= "ext";
$result = mysql_query("SELECT * FROM extensions ORDER BY '$orderby'");
}

$num=mysql_numrows($result);


?>
...
<td class="1"><b><a href="view.php?orderby=ext">Ext</a></b></td>
<td class="1"><b><a href="view.php?orderby=building">Building</a></b></td>
<td class="1"><b><a href="view.php?orderby=type">Phone</a></a></b></td>
<td class="1"><b><a href="view.php?orderby=room">Room</b></td>
<td class="1"><b><a href="view.php?orderby=floor">Floor</a></b></td>
<td class="1"><b><a href="view.php?orderby=elevation">Elevation</a></b></td>
</tr>

<?
$i=0;
$i2 = 1;
while ($i < $num) {

if ($i2 % 2 == 0 ) {
$incl = "class=\"td1\"";
}
else {
$incl = "class=\"td2\"";
}

$ext=mysql_result($result,$i,"ext");
$building=mysql_result($result,$i,"building");
$type=mysql_result($result,$i,"type");
$room=mysql_result($result,$i,"room");
$floor=mysql_result($result,$i,"floor");
$el=mysql_result($result,$i,"elevation");

echo "<tr>
<td $incl>$ext</td>
<td $incl>$building</td>
<td $incl>$type</td>
<td $incl>$room</td>
<td $incl>$floor</td>
<td $incl>$el</td>
</tr>
";

$i++;
$i2++;
}

mysql_close();

?>

The weird thing is, the sort feature works on the export page. I copied the code verbatim from there and it still won't go. Any help would be greatly appreciated. Thanks.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 14 June 2006 - 01:57 PM

I don't see where you're extracting the "orderby" part from the URL.

You should be using $_GET['orderby'] or $_REQUEST['orderby'] instead of $orderby.

Info: PHP Manual


#3 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 01:59 PM

order by `field` what? desc, asc?
z..z..z..z..z..z..z..z..

#4 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:00 PM

Yeah, I should probably use a GET but it still works nonetheless; Is something like "view.php?orderby=room" - as I have now - not sufficient or reliable? I'm fairly certain that's not what's causing the problem, though...

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 14 June 2006 - 02:03 PM

If the server has been changed to disallow global variables (like if they upgraded PHP, for example) then you can't get the variables you're talking about without using the method I mentioned. And I'm fairly certain that WOULD be the problem.

Info: PHP Manual


#6 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:03 PM

[!--quoteo(post=383780:date=Jun 14 2006, 09:59 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 14 2006, 09:59 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
order by `field` what? desc, asc?
[/quote]

I just changed the query line to
$result = mysql_query("SELECT * FROM extensions ORDER BY '$orderby' $sort");
and although $sort does exist, it did nothing.


Thanks for the suggestions all. Keep 'em coming.

#7 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 14 June 2006 - 02:04 PM

$result = mysql_query("SELECT * FROM extensions ORDER BY '" . $_GET['orderby'] . "' $sort");

Info: PHP Manual


#8 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:06 PM

Just threw that code in, thanks Ober. No change though.

I feel like I'm missing something really stupid.

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 June 2006 - 02:11 PM

As a debugging aid, echo the query ahead of the table display. At least then you will know for certain just what query is being run - and it may not be the query that you expect.
Legend has it that reading the manual never killed anyone.
My site

#10 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:17 PM

Just for reference, here's my export.php file, where the sort currently is working.

<?
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=extensions.xls");
header("Pragma: no-cache");
header("Expires: 0"); 

$tab = "\t";
$cr = "\n";

$username="asdf";
$password="sdfsdf";
$database="asdfsdfcom";
$host="ssdf.net";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$data = "Extension" . $tab . "Building" . $tab . "Line" . $tab . "Room" . $tab . "Floor" . $tab . "Elevation" . $cr;

   $result = mysql_query("SELECT * FROM extensions ORDER BY '$orderby'");

   while($row = mysql_fetch_array($result)) { 
   $data .= $row[ext] . $tab . $row[building] . $tab .  $row[type] . $tab . $row[room] . $tab .  $row[floor] . $tab . $row[elevation] .$cr;
             }
mysql_close();
echo $data; 

     ?>

[!--quoteo(post=383797:date=Jun 14 2006, 10:11 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 14 2006, 10:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
As a debugging aid, echo the query ahead of the table display. At least then you will know for certain just what query is being run - and it may not be the query that you expect.1
[/quote]
Good idea. I had done that initially, and it looked right, but now $query no longer exists, because I'm using
$result = mysql_query("SELECT * FROM extensions ORDER BY '" . $_GET['orderby'] . "' $sort");
in lieu of it.

[!--quoteo(post=383797:date=Jun 14 2006, 10:15 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 14 2006, 10:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$num=mysql_numrows($result);

change to

$num=mysql_num_rows($result);
[/quote]

Done.

#11 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 02:18 PM

sorry there were some multiple posts of mine I deleted every post though hmm. anyway
in the first piece of code

$num=mysql_numrows($result);

change to

$num=mysql_num_rows($result);

never neglect the little things
z..z..z..z..z..z..z..z..

#12 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 14 June 2006 - 02:18 PM

I'd suggest splitting it out so you can echo the query. Rule #1 of debugging, always keep your query outside of the mysql_query function so you can echo it.

Info: PHP Manual


#13 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:20 PM

[!--quoteo(post=383797:date=Jun 14 2006, 10:11 AM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 14 2006, 10:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
As a debugging aid, echo the query ahead of the table display. At least then you will know for certain just what query is being run - and it may not be the query that you expect.
[/quote]
Good idea. I had done that initially, and it looked right, but now $query no longer exists, because I'm using
$result = mysql_query("SELECT * FROM extensions ORDER BY '" . $_GET['orderby'] . "' $sort");
in lieu of it.

#14 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 02:22 PM

ehhhhh....ORDER BY `$field` DESC not ORDER BY '$field' DESC it's a field not a value
z..z..z..z..z..z..z..z..

#15 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:29 PM

Okay, if you go back to the page, I set the sqlquery to call the $query variable :

if (!$orderby){
$query = "SELECT * FROM extensions ORDER BY 'ext' $sort";
$result = mysql_query($query);
echo "!, $query";
}

if ($orderby){
$orderby= "ext";
$query = "SELECT * FROM extensions ORDER BY '" . $_GET['orderby']. "' $sort";
$result = mysql_query($query);
echo "$query";
}



#16 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 02:31 PM

"SELECT * FROM extensions ORDER BY '" . $_GET['orderby']. "' $sort";

should be

"SELECT * FROM extensions ORDER BY `" . $_GET['orderby']. "` $sort";
z..z..z..z..z..z..z..z..

#17 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:34 PM

Done. And did nothing. :(

I appreciate everyone's help on this. I wish this stupid junk was easier.

#18 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 02:38 PM

where are you getting $orderby from in export.php?
z..z..z..z..z..z..z..z..

#19 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 14 June 2006 - 02:40 PM

The URL. I haven't cleaned that page up yet. I suppose it is prone to errors..

#20 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 June 2006 - 02:51 PM

[!--quoteo(post=383828:date=Jun 14 2006, 10:40 AM:name=alexruimy)--][div class=\'quotetop\']QUOTE(alexruimy @ Jun 14 2006, 10:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The URL. I haven't cleaned that page up yet. I suppose it is prone to errors..
[/quote]
Why should it be? Since the parameter you want is passed by URL, retrieve it (properly) by using syntax like:
$orderby = $_GET['orderby'];

Unless and until you actually echo the query along with the results table, you'll never be sure that you really have the query you expect. "Did nothing" suggests to me that there's an error in your code logic and/or what's online is different from the version you're working with.

Sort out retrieving $orderby, echo the query, add sql error trapping, upload to the site and let's see what's really happening.

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users