note 68891 added to function.mysqli-prepare
| From: | j dot andrew dot mcentire at gmail dot com | Date: | Tue, 15 Aug 2006 03:37:59 +0000 |
| Subject: | note 68891 added to function.mysqli-prepare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115921@lists.php.net to get a copy of this message | ||
Advancing on my previous note to modify the mysqli_stmt and mysqli classes; I've coded the
following to allow the passing of an array rather than a list of variables to
mysqli_stmt_bind_param. As written, it is for my DB class created specifically for inserts, but is
extensible to accomidate various uses. For the dynamic quality of my website, I wanted a function
that would allow me to fetch and insert variable numbers of values from and into a table:
<?php
class PS extends mysqli_stmt { };
class DB extends mysqli {
public function type ( $var ) {
switch ( gettype( $var ) ) {
case 'integer':
return 'i';
break;
case 'double':
return 'd';
break;
case 'string':
return 's';
break;
case 'boolean':
case 'array':
case 'object':
case 'resource':
case 'NULL':
default:
return NULL;
break;
} // could re-write to return first character, or remove breaks. Meh.
public function arr_to_arg ( $array ) {
$keys = $vals = $type = $args = NULL;
foreach ( $array as $key => $val ) {
$keys .= $key . ", ";
$vals .= "?, ";
$type .= $this->type( $vals );
$args .= '$array[\'' . $key . '\'], ';
}
$keys = rtrim( $keys, ", " );
$vals = rtrim( $vals, ", " );
$type = "'" . $type . "', ";
$args = $type . rtrim( $args, ", " );
return array( $keys, $vals, $args );
}
public function prepared_insert ( $table, $array ) {
list( $keys, $vals, $args ) = $this->arr_to_arg( $array );
$query = "INSERT INTO ${table} (${keys}) VALUES (${vals})";
$PS = new PS( $this, $query );
$line = "\$PS->bind_param( $args );";
eval ( $line );
$PS->execute( );
$PS->close( );
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 68.97.15.32
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-prepare.php
Edit -- https://master.php.net/note/edit/68891
Del: integrated -- https://master.php.net/note/delete/68891/integrated
Del: useless -- https://master.php.net/note/delete/68891/useless
Del: bad code -- https://master.php.net/note/delete/68891/bad+code
Del: spam -- https://master.php.net/note/delete/68891/spam
Del: non-english -- https://master.php.net/note/delete/68891/non-english
Del: in docs -- https://master.php.net/note/delete/68891/in+docs
Del: other reasons-- https://master.php.net/note/delete/68891
Reject -- https://master.php.net/note/reject/68891
Search -- https://master.php.net/manage/user-notes.php