0

I'm having a small problem with my PHP code... I've searched on the forum and copied the code but I must be doing something wrong. I'm trying to order the results I'm getting on a page I have by name, year etc. using a variable. But it isn't working?

I've got this:

$orderby = 'name';    //'$_GET['orderby']' <---- I'd like to use this later to make it dynamic
$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`'$orderby'` ASC');

And why isn't the code working? It doesn't give me an error, the page just doesn't load and stays blank!

1
  • What, no rants about using $_GET in queries and SQL injection yet? :) Commented Dec 28, 2013 at 10:55

3 Answers 3

2

You forgot the concatenation :

$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`'.$orderby.'` ASC');

Or

$req = mysql_query("select id, name, year, genre, cover from table ORDER BY  `table`.`$orderby` ASC");
Sign up to request clarification or add additional context in comments.

Comments

1

Change '$orderby' to '.$orderby.'. . is php concatenation operator.

Comments

0

Try

$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`' . $orderby . '` ASC');

or

$req = mysql_query('select id, name, year, genre, cover from table ORDER BY  `table`.`$orderby` ASC');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.