Jump to content

Recommended Posts

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.

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 $$;

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

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.

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'];

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.