babiez4sale Posted June 10, 2011 Share Posted June 10, 2011 This has me quite baffled. I have a query that pulls all entries from a table and then displays them based on the limits specified by the pagination. Pretty simple. $query = "SELECT * FROM `entries` LIMIT ".$start.", ".$maxLimit.";"; I now want those entries to be ordered from newest to oldest, so I change the query as such: $query = "SELECT * FROM `entries` ORDER BY `id` DESC LIMIT ".$start.", ".$maxLimit.";"; This causes the script to not fail; it never executes. When I point my browser to the script, index.php, firefox prompts me to save the file or open with another application. This is the weird part. If I change "DESC" to "ASC" (or just remove the ORDER BY clause in general), the entire script works perfectly again and the website gets built by just pointing to the directory (like it should by calling index.php). $query = "SELECT * FROM `entries` ORDER BY `id` ASC LIMIT ".$start.", ".$maxLimit.";"; //this works just fine This is on my local server running Apache2, PHP5, and mysql 5.1. Any help is much appreciated because I'm at a loss. Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/ Share on other sites More sharing options...
kenrbnsn Posted June 10, 2011 Share Posted June 10, 2011 We need to see more of your code. Please post your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228100 Share on other sites More sharing options...
babiez4sale Posted June 11, 2011 Author Share Posted June 11, 2011 Alright, I'm sorry, I tracked it down to a single line where I mistakenly typed a variable as $$var. Can someone explain to me why, for example, this causes the script to fail without a compile error, or even an E_NOTICE or E_WARNING echo $$; Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228188 Share on other sites More sharing options...
jcbones Posted June 11, 2011 Share Posted June 11, 2011 Because $$var is valid syntax. It is called variable variable. Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228189 Share on other sites More sharing options...
babiez4sale Posted June 11, 2011 Author Share Posted June 11, 2011 My bad, I just found that in the php docs. I'm unsure of the proper use because I've never used one. The php.net docs don't do the best job of explaining it. Thanks guys, sorry to sound like such a noob. Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228190 Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 My bad, I just found that in the php docs. I'm unsure of the proper use because I've never used one. The php.net docs don't do the best job of explaining it. Thanks guys, sorry to sound like such a noob. you're not a noob you're simply learning, and we are here to help Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228193 Share on other sites More sharing options...
babiez4sale Posted June 11, 2011 Author Share Posted June 11, 2011 thanks. I've always respected the community here at PHPFreaks. I'm a pretty fluent PHP programmer, but there's always something new that can be learned. edit: if anyone wonders, this portion I was having issues with passes data off to an method that constructs the HTML formatting for each entry. Though I found the issue to be with the mistaken variable variable, I'm still uncertain as to why using ORDER BY ASC didn't cause the error when DESC did. I'd display my code, but it's written as a modular web app and there are several different objects required and I don't want to post hundreds of lines of code. Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228195 Share on other sites More sharing options...
jcbones Posted June 11, 2011 Share Posted June 11, 2011 My bad, I just found that in the php docs. I'm unsure of the proper use because I've never used one. The php.net docs don't do the best job of explaining it. Thanks guys, sorry to sound like such a noob. Here is a use of it: (demo only, not recommended). //$_POST array contains 'username' and 'password'. foreach($_POST as $key => $value) { $$key = $value; //variable variable. } //same as: extract($_POST); //same as list($username,$password) = $_POST; //same as $username = $_POST['username']; $password = $_POST['password']; Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228307 Share on other sites More sharing options...
PFMaBiSmAd Posted June 11, 2011 Share Posted June 11, 2011 The inadvertent variable variable probably overwrote one of your actual program variables, created an empty variable, or referenced a nonexistent variable and caused your logic to skip executing the code where the query is at. Without the actual code showing the mis-typed variable and how it relates to the the query or to the execution of the query, this is just a game of 20 guesses. As to variable variables, they are undesirable for several reasons - they are three times slower than using an array variable (most people use them to create a series of named/numbered variables for a SET of related data and you should be using an array in this case anyway), they allow program variables to be overwritten (either accidentally by the programmer or intentionally by a hacker feeding all kinds of external data to your code via $_POST/$_GET data), they make your code difficult to read a few months after you have written it (or by someone who did not write it) because you don't know exactly what variables there are, where they got set, or what they got set to (see if you can easily tell what the code at the following link is looping over so that you could debug it when it does not work - http://www.phpfreaks.com/forums/index.php?topic=335615.msg1581068#msg1581068). Quote Link to comment https://forums.phpfreaks.com/topic/239008-unexpected-behavior-using-order-by-ascdesc/#findComment-1228358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.