Eric
Eric

Reputation: 111

How to escape # in PHP

How can I escape the # in PHP? When I use it in a query it turns the remaining line of code into a comment. This is what I have right now:

$columns = "head1, #_search, #_stuff";
$result = mysql_query("SELECT $columns from table LIMIT $k,$j");

I can't just put escape # right after $columns since it will just become a comment.

~edit: yes I probably should have copied the code directly, but some of it is confidential and much more complicated.

Upvotes: 2

Views: 163

Answers (4)

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

How about the following ? :

$columns = "head1, `#_search`, `#_stuff`";

You can use `(backtick) to quote reserved words.

If you want to quote table/column name you can do the following example:

 SELECT * FROM `#table1` WHERE `#table1`.`#column1` = 1;

Reference

Upvotes: 1

Anjan Biswas
Anjan Biswas

Reputation: 7922

Try adding backslashes

$columns = 'head1, \#_search, \#_stuff';

Upvotes: 2

Guffa
Guffa

Reputation: 700342

Shouldn't it be a string literal?

$columns = 'head1, #_search, #_stuff'

Upvotes: 1

Martin
Martin

Reputation: 6687

You should be using quotes for your strings:

$columns = 'head1, #_search, #_stuff';

However, this still doesn't make much sense.

It's also recommended to favour PDO or mysqli over mysql_*

Upvotes: 3

Related Questions