
Originally Posted by
r937
not in mysql, no
but it is a very bad habit to get into, because this "skill" (lazily and haphazardly quoting stuff) will ~not~ work in other databases
Yes exactly.
In order to make abstraction so that it works for all db layers.
we can use some utility function. For example:
PHP Code:
function quote_smart($value){
$value = stripslashes($value);
if (!is_numeric($value)){
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
//usage
$sql = "SELECT * FROM table WHERE field=".quote_smart($value);
In the meanwhile i found one (must say better) code :
PHP Code:
<?php
function quote_smart($values, $quotes = true) {
if (is_array($values)) {
foreach ($values as $key => $value) {
$values[$key] = quote_smart($value, $quotes);
}
}
else if ($values === null) {
$values = 'NULL';
}
else if (is_bool($values)) {
$values = $values ? 1 : 0;
}
else if (!is_numeric($values)) {
$values = mysql_real_escape_string($values);
if ($quotes) {
$values = '"' . $values . '"';
}
}
return $values;
}
?>
isn't this a better way to go?
Bookmarks