Major_Disaster wrote:So im wondering, is there something wrong with my SQL? Specifically the LIKE part?
I believe the problem is that the '%' wildcards need to be part of the parameter you bind to the variable, not in the SQL query being prepared. In other words, when you bind the variable, you'd bind something like:
'%' . $user_data . '%'
rather than placing the %'s in the SQL query string.
Major_Disaster wrote:With regards to prepared statements - im completely new to all this and worried about making rookie security mistakes. I read prepared statements helped prevent SQL injection so blindly tried using them.
They help in that they automatically sanitize/escape the data being bound to the parameters in the statement. They offer no more security than raw queries where you manually sanitize/escape the data before placing it directly into the SQL query string.
In other words, your query string above could be written (with proper sanitization/escaping) like so:
$query = sprintf("SELECT * FROM `stock_search` WHERE `part_number` LIKE '%s'",
mysqli_real_escape_string($link, $part_search)
);
(Note that the use of [man]sprintf/man certainly isn't required - I just like to use it when building SQL query strings as I find it helps keep the code cleaner and also allows you to do explicit type-casting for data sanitization when dealing with numeric data types.)
Major_Disaster wrote:The statement will not be reused - so should i abandon the code?
Like I said above, that might be a matter of preference. I'm no SQL/DB guru, but in my personal opinion/experience, using prepared statements for queries that are only executed one time were a waste of typing/code space and/or resources. Writing the code to prepare a statement, bind my input variables, bind my output variables, execute the statement, etc. all seemed laborious as compared to just executing a raw SQL query string an fetching the results.
Are prepared statements worth learning and/or beneficial in certain situations? Of course! Do I think they are always more beneficial? Nope.
Major_Disaster wrote:I was following the prepared statement example here, should i switch to the "Basic Usage Example"?
Eh... that's up to you. There are a couple of phrases/"facts" in that article that I disagree with (in intensities varying from mild to quite strong).
For example, the author of that article writes:
SECURITY!!!!!!!!:
Since the binary protocol in mysql has changed, and now supports prepared statements, this gives you an extremely secure method for executing queries. This completely elminated the possibility
of SQL Injections.
The elimination of SQL injection attacks can easily be accomplished using the mysql PHP extension provided that the programmer has properly educated himself/herself in the topic of security and SQL. He seems to imply that this wasn't possible until MySQLi was released, which is certainly false.
The thing that I don't like is that everyone seems to think that using prepared statements is so much better because you don't have to think about the security involved. That may be true, but I don't think that's beneficial at all. I still think the programmer should know why prepared statements are safer, e.g. what happens that makes them safe.