note 101952 deleted from mysqli-stmt.bind-param by danbrown
| From: | danbrown@php.net | Date: | Wed, 19 Jan 2011 23:07:20 +0000 |
| Subject: | note 101952 deleted from mysqli-stmt.bind-param by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175917@lists.php.net to get a copy of this message | ||
Note Submitter: renatoly at gmail dot com
----
Based on the function wrote by canche_x at yahoo dot com, I wrote this new function that is a bit
more clear. Specially when executing select, show vs insert, updates & deletes:
Example of how to use it:
$statement = "Select 1 as result from userscashier where idcashier=? and idsala=? and
username=? and password=? and ip=?";
$resultset = $dbhandler::preparedQuery($statement,
array('iisss',$pidCashier,$pidSala,$puserName,$ppassword,$pip), TRUE);
echo '<br \>the number of records found: ' . $resultset;
if ($resultset) {
$statement = "Select nextval('idAuthSN') as idAuthSN";
$resultset = $dbhandler::preparedQuery($statement,array(), TRUE, $rs);
echo '<br \> Pepe pepe pepe The idauthsn is ' . $rs[0]['idAuthSN'];
$this->setAuthSN($rs[0]['idAuthSN']);
$this->setAuthSignature(sha1($rs[0]['idAuthSN'] . CsUtilities::SECKEY));
//echo 'the secret code is ' . CsUtilities::SECKEY;
$statement = "Insert into authenticatedcashiers values (?,?)";
$resultset = $dbhandler::preparedQuery($statement,array('ss',$this->getAuthSN(),
$this->getAuthSignature()), FALSE);
echo "<br \> Number of inserted rows $resultset <br \>";
return true;
}
public static function preparedQuery ($statement, $params, $select=TRUE, &$rs=NULL) {
$mysqli = self::getConnection();
if ($stmt = $mysqli->prepare($statement)) {
if (count($params)>0)
call_user_func_array(array($stmt, 'bind_param'), self::refValues($params));
$stmt->execute();
if(!$select) {
$result = $mysqli->affected_rows;
}
else {
$meta = $stmt->result_metadata();
if ($meta) {
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), self::refValues($parameters));
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
$rs = $results;
$result = count($results);
}
else
$result = 0;
}
$stmt->close();
return $result;
}
}