BernardA
BernardA

Reputation: 1523

php how to refer to variable in a ORDER BY FIELD()

I need to dynamically order a table using a variable.

A var_dump of an instance of the variable $vid is:

string(9) "15,251,21"

And this is an example of the query:

$sql=mysql_query("SELECT * FROM vcomp ORDER BY FIELD(version_id,$vid)");

It does work if I place the hard numbers, like:

    $sql=mysql_query("SELECT * FROM vcomp ORDER BY FIELD(version_id,15, 251, 21)");

I tried quotes, backtick and the like without success. Thanks

Upvotes: 1

Views: 309

Answers (1)

Kalpesh
Kalpesh

Reputation: 5685

Try this then:

$ord = "version_id, ".$vid;
$sql=mysql_query("SELECT * FROM vcomp ORDER BY FIELD($ord)");

Upvotes: 1

Related Questions