lusketeer
lusketeer

Reputation: 1930

Short Code for PHP string with variable

I have a query string that contains a variable like this

$field_name = 'features';
$value = '5';
$query = "SELECT * FROM Table WHERE $field_name\_tid = '$value'";

My goal is to print out the $query like this SELECT * FROM Table WHERE features_tid = '5'; I put \_ there hoping it would work as escape character, but it didn't work. Is there any way to achieve this without use methods like ". $field_name ." and modifying original variable value?

Upvotes: 1

Views: 123

Answers (2)

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6224

You can use:

$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";

Upvotes: 1

mohammad falahat
mohammad falahat

Reputation: 748

yes:

$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";

Upvotes: 2

Related Questions