Reputation: 44842
I've got a variable in a php function of mine which is passed into a SQL query. I'm wondering how do I append " and " either side of it without actually turning it into a string?
Thanks.
Upvotes: 0
Views: 103
Reputation: 6637
To answer your question:
$name = "Peter";
$query = " SELECT * FROM table WHERE name LIKE '$name' ";
All previous comments/answers to your question are valid and should be taken into account. Always remember that prepared statements are good if you want to avoid SQL Injection related problems.
Upvotes: 2
Reputation: 526503
If you can, you should really look into using prepared statements. In PHP both the mysqli
and PDO
libraries support them. This allows you to create placeholders in your queries and then bind values to them without having to manipulate the SQL text itself.
By doing this, you both save yourself hassle in terms of getting the SQL formatting right, and also protect yourself against accidental SQL injection holes by no longer having to remember to escape strings.
Upvotes: 3