I am trying to use PDO for my new projects and I can't seem to understand or at least find a good example on prepared statements, I have an update query that works fine but I know I'm not using it right for the data sanitation feature that PDO offers, this is my code:
You want to put your ":text" place-holders in the query string itself where you currently have $_POST values. Then when you "bind" values to those parameters, PDO will take care of the sanitation for you. Much simplified:
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I made the changes and I passed a value like "O'reilly" to the DB and it didn't really convert it to anything, it went into the table as "O'reilly", am I missing something here?
I made the changes and I passed a value like "O'reilly" to the DB and it didn't really convert it to anything, it went into the table as "O'reilly", am I missing something here?
Thanks.
That means it worked.
You don't want to convert it to anything in the actual data. Whatever escaping happens is only for the actual query string so that the MySQL parser knows to treat it as a literal character, much like you might us a "\" to escape a literal quote in a PHP echo:
PHP Code:
echo "\"This is a test,\" he said";
Which will output:
Code:
"This is a test," he said
...without those backslashes.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
As you can see in my original code I am updating two tables from within the same query, no matter what I do, this $count = $sql->rowCount(); will always return 0 (zero), is this the best method to get the number of affected rows here?
As far as I know, that should work okay for an insert or update query in MySQL (but it may not work for select queries). If it's not working for you, we may need to see exactly where and how are are calling it.
PS: You may want to check if it's actually returning 0 or false:
PHP Code:
$count = $sql->rowCount(); if($count === false) { // note use of "===" operator, not "==", to differentiate 0 and FALSE //some debug code here }
Last edited by NogDog; 02-04-2013 at 03:48 PM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks