note 62497 added to function.mysql-query
| From: | gconnor at sgi dot com | Date: | Thu, 02 Mar 2006 01:19:04 +0000 |
| Subject: | note 62497 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104661@lists.php.net to get a copy of this message | ||
I happily grabbed and used the "mysql_prepare()" function given in the first note above.
All is well.
I made a slight tweak so that I could feed in NULL values without getting an empty string (or 0)
instead.
// mysql_query() wrapper. takes two arguments. first
// is the query with '?' placeholders in it. second argument
// is an array containing the values to substitute in place
// of the placeholders (in order, of course).
// Pass NULL constant in array to get unquoted word NULL
function mysql_prepare ($query, $phs = array()) {
foreach ($phs as $ph) {
if ( isset($ph) ) {
$ph = "'" . mysql_real_escape_string($ph) . "'";
} else {
$ph = "NULL" ;
}
$query = substr_replace(
$query, $ph, strpos($query, '?'), 1
);
}
return mysql_query($query);
}
Sample function call:
// Check to see if all variables are defined
if ( isset($f_hostid,$f_eventid,$f_owner,$f_comment) ) {
// For numeric values, blank means NULL
if ( $f_eventid=="" ) { $f_eventid = NULL ; }
$result = mysql_prepare(
'UPDATE Hosts SET event_id=?, owner=?, comment=? WHERE id=?',
array( $f_eventid,$f_owner,$f_comment, $f_hostid )
);
if (!$result) {
$message = 'Error while updating: ' . mysql_error() .
"<br />\n";
die($message);
}
echo "Update successful. <br />\n" ;
} else {
echo "Missing value. Update failed... check form logic. <br />\n" ;
}
----
Server IP: 64.71.164.2
Probable Submitter: 192.48.171.17 (proxied: 1.0 proxy-mtv1:8080 (DataReactor/4.0.5))
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- http://master.php.net/note/edit/62497
Del: integrated -- http://master.php.net/note/delete/62497/integrated
Del: useless -- http://master.php.net/note/delete/62497/useless
Del: bad code -- http://master.php.net/note/delete/62497/bad+code
Del: spam -- http://master.php.net/note/delete/62497/spam
Del: non-english -- http://master.php.net/note/delete/62497/non-english
Del: in docs -- http://master.php.net/note/delete/62497/in+docs
Del: other reasons-- http://master.php.net/note/delete/62497
Reject -- http://master.php.net/note/reject/62497
Search -- http://master.php.net/manage/user-notes.php