public function closeCursor() { if ($this->_result) { $this->fetch_fields = array(); $this->fetch_out = null; mysqli_stmt_free_result($this->_result); } }
function mysqli_fetch_array_large($offset, $link, $package_size) { /* we are aiming for maximum compression to test MYSQLI_CLIENT_COMPRESS */ $random_char = str_repeat('a', 255); $sql = "INSERT INTO test(label) VALUES "; while (strlen($sql) < $package_size - 259) { $sql .= sprintf("('%s'), ", $random_char); } $sql = substr($sql, 0, -2); $len = strlen($sql); assert($len < $package_size); if (!@mysqli_query($link, $sql)) { if (1153 == mysqli_errno($link) || 2006 == mysqli_errno($link) || stristr(mysqli_error($link), 'max_allowed_packet')) { /* myslqnd - [1153] Got a packet bigger than 'max_allowed_packet' bytes libmysql -[2006] MySQL server has gone away */ return false; } printf("[%03d + 1] len = %d, [%d] %s\n", $offset, $len, mysqli_errno($link), mysqli_error($link)); return false; } /* buffered result set - let's hope we do not run into PHP memory limit... */ if (!($res = mysqli_query($link, "SELECT id, label FROM test"))) { printf("[%03d + 2] len = %d, [%d] %s\n", $offset, $len, mysqli_errno($link), mysqli_error($link)); return false; } while ($row = mysqli_fetch_assoc($res)) { if ($row['label'] != $random_char) { printf("[%03d + 3] Wrong results - expecting '%s' got '%s', len = %d, [%d] %s\n", $offset, $random_char, $row['label'], $len, mysqli_errno($link), mysqli_error($link)); return false; } } mysqli_free_result($res); if (!($stmt = mysqli_prepare($link, "SELECT id, label FROM test"))) { printf("[%03d + 4] len = %d, [%d] %s\n", $offset, $len, mysqli_errno($link), mysqli_error($link)); return false; } /* unbuffered result set */ if (!mysqli_stmt_execute($stmt)) { printf("[%03d + 5] len = %d, [%d] %s, [%d] %s\n", $offset, $len, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), mysqli_errno($link), mysqli_error($link)); return false; } $id = $label = NULL; if (!mysqli_stmt_bind_result($stmt, $id, $label)) { printf("[%03d + 6] len = %d, [%d] %s, [%d] %s\n", $offset, $len, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), mysqli_errno($link), mysqli_error($link)); return false; } while (mysqli_stmt_fetch($stmt)) { if ($label != $random_char) { printf("[%03d + 7] Wrong results - expecting '%s' got '%s', len = %d, [%d] %s\n", $offset, $random_char, $label, $len, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } } mysqli_stmt_free_result($stmt); mysqli_stmt_close($stmt); return true; }
function queueNotify($type, $data) { $connection = mappedConnection('queue'); $sql = 'INSERT INTO queue (event) VALUES (?);'; $statement = mysqli_prepare($connection, $sql); $value = serialize(['type' => $type, 'data' => $data]); mysqli_stmt_bind_param($statement, 's', $value); mysqli_stmt_execute($statement); mysqli_stmt_free_result($statement); }
function insertLogingegevens($PersoonID, $Gebruikersnaam, $Wachtwoord) { $link = connect(); $stmt = mysqli_prepare($link, "INSERT INTO Logingegevens(PersoonID, Gebruikersnaam, Wachtwoord) VALUES(?, ?, ?);"); mysqli_stmt_bind_param($stmt, "iss", $PersoonID, $Gebruikersnaam, $Wachtwoord); mysqli_stmt_execute($stmt); mysqli_stmt_free_result($stmt); mysqli_stmt_close($stmt); mysqli_close($link); }
function test_format($link, $format, $from, $order_by, $expected, $offset) { if (!($stmt = mysqli_stmt_init($link))) { printf("[%03d] Cannot create PS, [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link)); return false; } if ($order_by) { $sql = sprintf('SELECT %s AS _format FROM %s ORDER BY %s', $format, $from, $order_by); } else { $sql = sprintf('SELECT %s AS _format FROM %s', $format, $from); } if (!mysqli_stmt_prepare($stmt, $sql)) { printf("[%03d] Cannot prepare PS, [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_execute($stmt)) { printf("[%03d] Cannot execute PS, [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_store_result($stmt)) { printf("[%03d] Cannot store result set, [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!is_array($expected)) { $result = null; if (!mysqli_stmt_bind_result($stmt, $result)) { printf("[%03d] Cannot bind result, [%d] %s\n", $offset + 4, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_fetch($stmt)) { printf("[%03d] Cannot fetch result,, [%d] %s\n", $offset + 5, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if ($result !== $expected) { printf("[%03d] Expecting %s/%s got %s/%s with %s - %s.\n", $offset + 6, gettype($expected), $expected, gettype($result), $result, $format, $sql); } } else { $order_by_col = $result = null; if (!mysqli_stmt_bind_result($stmt, $order_by_col, $result)) { printf("[%03d] Cannot bind result, [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } reset($expected); while ((list($k, $v) = each($expected)) && mysqli_stmt_fetch($stmt)) { if ($result !== $v) { printf("[%03d] Row %d - expecting %s/%s got %s/%s [%s] with %s - %s.\n", $offset + 8, $k, gettype($v), $v, gettype($result), $result, $order_by_col, $format, $sql); } } } mysqli_stmt_free_result($stmt); mysqli_stmt_close($stmt); return true; }
/** * @param string $query * @param string $types * @param ...$params * * @return array|null */ function executeQuery($query, $types = null, ...$params) { if ($types !== null) { $stmt = mysqli_prepare(getConnection(), $query); if (!mysqli_stmt_bind_param($stmt, $types, ...$params)) { die('Could not bind query params.'); } if (!mysqli_stmt_execute($stmt)) { die('Could not execute mysqli statement.'); } $result = mysqli_stmt_get_result($stmt); mysqli_stmt_free_result($stmt); return resultQuery($result); } $result = mysqli_query(getConnection(), $query); return resultQuery($result); }
function zerofill($offset, $link, $datatype, $insert = 1) { mysqli_query($link, 'ALTER TABLE test_mysqli_stmt_bind_result_zerofill_table_1 DROP zero'); $sql = sprintf('ALTER TABLE test_mysqli_stmt_bind_result_zerofill_table_1 ADD zero %s UNSIGNED ZEROFILL', $datatype); if (!mysqli_query($link, $sql)) { // no worries - server might not support it return true; } if (!mysqli_query($link, sprintf('UPDATE test_mysqli_stmt_bind_result_zerofill_table_1 SET zero = %s', $insert))) { printf("[%03d] UPDATE failed, [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link)); return false; } if (!($stmt = mysqli_prepare($link, 'SELECT zero FROM test_mysqli_stmt_bind_result_zerofill_table_1 LIMIT 1'))) { printf("[%03d] SELECT failed, [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link)); return false; } $result = null; if (!mysqli_stmt_bind_result($stmt, $result)) { printf("[%03d] Bind failed, [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_execute($stmt) || !mysqli_stmt_fetch($stmt)) { printf("[%03d] Execute or fetch failed, [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } $res = mysqli_stmt_result_metadata($stmt); $meta = mysqli_fetch_fields($res); mysqli_stmt_free_result($stmt); $meta = $meta[0]; $length = $meta->length; if ($length > strlen($insert)) { $expected = str_repeat('0', $length - strlen($insert)); $expected .= $insert; if ($expected !== $result) { printf("[%03d] Expecting '%s' got '%s'\n", $offset, $expected, $result); return false; } } else { if ($length <= 1) { printf("[%03d] Length reported is too small to run test\n", $offset); return false; } } return true; }
function func_test_mysqli_stmt_num_rows($stmt, $query, $expected, $offset) { if (!mysqli_stmt_prepare($stmt, $query)) { printf("[%03d] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_execute($stmt)) { printf("[%03d] [%d] %s\n", $offset + 1, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if (!mysqli_stmt_store_result($stmt)) { printf("[%03d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } if ($expected !== ($tmp = mysqli_stmt_num_rows($stmt))) { printf("[%03d] Expecting %s/%d, got %s/%d\n", $offset + 3, gettype($expected), $expected, gettype($tmp), $tmp); } mysqli_stmt_free_result($stmt); return true; }
/** * Returns $numItems rows starting from the $startIndex row from the * table. * * Add authorization or any logical checks for secure access to your data * * * * @return array */ public function getReviews_paged($startIndex, $numItems) { //mysqli_query($this->connection, "set names utf8"); $stmt = mysqli_prepare($this->connection, "SELECT * FROM {$this->tablename} LIMIT ?, ?"); $this->throwExceptionOnError(); mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->id, $row->restaurantName, $row->icon, $row->starRating, $row->starImageLoc, $row->reviewSubmitDate, $row->userName, $row->description); while (mysqli_stmt_fetch($stmt)) { $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->id, $row->restaurantName, $row->icon, $row->starRating, $row->starImageLoc, $row->reviewSubmitDate, $row->userName, $row->description); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; }
public function close() { mysqli_stmt_free_result($this->res); mysqli_stmt_close($this->res); }
<?php require_once 'connectdb.php'; session_start(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = trim($_POST["username"]); $password = trim($_POST["password"]); try { $sql = "SELECT * FROM userprofile where userName='******' and passw='" . $password . "'"; if ($result = mysqli_query($connection, $sql)) { if ($row = mysqli_fetch_assoc($result)) { $userfullname = $row['name']; $userid = $row['userId']; $_SESSION['fname'] = $userfullname; $_SESSION['userid'] = $userid; $_SESSION['login'] = 1; header("location: ../home.php"); } else { $message = "Invalid user!"; echo "<script type='text/javascript'>alert('{$message}');window.location.href = '../home.php?error';</script>"; } mysqli_stmt_free_result($stmt); mysqli_close($connection); } } catch (Exception $e) { die(var_dump($e)); } }
function isWaiting($conn, $UserId, $BlockId) { $stmt = mysqli_prepare($conn, "select * from WaitingList where WUserid = ? and Blockid = ?"); mysqli_stmt_bind_param($stmt, "ii", $UserId, $BlockId); mysqli_stmt_execute($stmt); mysqli_stmt_store_result($stmt); $num = mysqli_stmt_num_rows($stmt); mysqli_stmt_free_result($stmt); mysqli_stmt_close($stmt); if ($num > 0) { return true; } return false; }
/** * Perform a query * @param string $queryText The prepared SQL statement that will be executed * @param bool|string $typeDef (Optional) The types of values that will be passed through the prepared statement. One letter per parameter * @param bool|array $params (Optional) The array of values that will be binded to the prepared statement * @return mixed Returns an array of the values received from the query or returns false on empty */ private function doQuery($queryText, $typeDef = false, $params = false) { $multiQuery = true; if ($stmt = $this->dbc->prepare($queryText)) { if (count($params) == count($params, 1)) { $params = array($params); $multiQuery = false; } if ($typeDef) { $bindParams = array(); $bindParamsReferences = array(); $bindParams = array_pad($bindParams, (count($params, 1) - count($params)) / count($params), ""); foreach ($bindParams as $key => $value) { $bindParamsReferences[$key] =& $bindParams[$key]; } array_unshift($bindParamsReferences, $typeDef); $bindParamsMethod = new ReflectionMethod('mysqli_stmt', 'bind_param'); $bindParamsMethod->invokeArgs($stmt, $bindParamsReferences); } $result = array(); foreach ($params as $queryKey => $query) { if ($typeDef) { foreach ($bindParams as $paramKey => $value) { $bindParams[$paramKey] = $query[$paramKey]; } } $queryResult = array(); if ($stmt->execute()) { $resultMetaData = $stmt->result_metadata(); $this->last_id = $stmt->insert_id; if ($resultMetaData) { $stmtRow = array(); $rowReferences = array(); while ($field = $resultMetaData->fetch_field()) { $rowReferences[] =& $stmtRow[$field->name]; } mysqli_free_result($resultMetaData); $bindResultMethod = new ReflectionMethod('mysqli_stmt', 'bind_result'); $bindResultMethod->invokeArgs($stmt, $rowReferences); while (mysqli_stmt_fetch($stmt)) { $row = array(); foreach ($stmtRow as $key => $value) { $row[$key] = $value; } $queryResult[] = $row; } mysqli_stmt_free_result($stmt); } else { $queryResult[] = mysqli_stmt_affected_rows($stmt); } } else { $this->error($this->dbc->error, $this->dbc->errno); $queryResult[] = false; } $result[$queryKey] = $queryResult; } mysqli_stmt_close($stmt); } else { $result = false; } if ($this->dbc->error) { $this->error($this->dbc->error, $this->dbc->errno); } if ($multiQuery) { return $result; } else { return $result[0]; } }
/** * Returns $numItems rows starting from the $startIndex row from the * table. * * Add authorization or any logical checks for secure access to your data * * * * @return array */ public function getTraceInTimeline_paged($startIndex, $numItems) { $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?"); $this->throwExceptionOnError(); mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->id, $row->idTimeline, $row->idTrace, $row->idSelector, $row->position, $row->delay, $row->visible); while (mysqli_stmt_fetch($stmt)) { $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->id, $row->idTimeline, $row->idTrace, $row->idSelector, $row->position, $row->delay, $row->visible); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; }
} if (NULL !== ($tmp = mysqli_stmt_free_result($stmt))) { printf("[008] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } if (false !== ($tmp = mysqli_stmt_store_result($stmt))) { printf("[009] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); } mysqli_stmt_close($stmt); if (!($stmt = mysqli_stmt_init($link))) { printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test_mysqli_stmt_free_result_table_1 ORDER BY id")) { printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } if (!mysqli_stmt_execute($stmt)) { printf("[012] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } if (true !== ($tmp = mysqli_stmt_store_result($stmt))) { printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp); } if (NULL !== ($tmp = mysqli_stmt_free_result($stmt))) { printf("[014] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } mysqli_stmt_close($stmt); if (NULL !== ($tmp = mysqli_stmt_free_result($stmt))) { printf("[015] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } mysqli_close($link); print "done!"; $test_table_name = 'test_mysqli_stmt_free_result_table_1'; require_once "clean_table.inc";
function mysqli_prepared_query($app, $link, $sql, $typeDef = FALSE, $params = FALSE) { if ($stmt = mysqli_prepare($link, $sql)) { if (count($params) == count($params, 1)) { $params = array($params); $multiQuery = FALSE; } else { $multiQuery = TRUE; } if ($typeDef) { $bindParams = array(); $bindParamsReferences = array(); $bindParams = array_pad($bindParams, (count($params, 1) - count($params)) / count($params), ""); foreach ($bindParams as $key => $value) { $bindParamsReferences[$key] =& $bindParams[$key]; } array_unshift($bindParamsReferences, $typeDef); $bindParamsMethod = new ReflectionMethod('mysqli_stmt', 'bind_param'); $bindParamsMethod->invokeArgs($stmt, $bindParamsReferences); } $result = array(); foreach ($params as $queryKey => $query) { foreach ($bindParams as $paramKey => $value) { $bindParams[$paramKey] = $query[$paramKey]; } $queryResult = array(); if (mysqli_stmt_execute($stmt)) { $resultMetaData = mysqli_stmt_result_metadata($stmt); if ($resultMetaData) { $stmtRow = array(); $rowReferences = array(); while ($field = mysqli_fetch_field($resultMetaData)) { $rowReferences[] =& $stmtRow[$field->name]; } mysqli_free_result($resultMetaData); $bindResultMethod = new ReflectionMethod('mysqli_stmt', 'bind_result'); $bindResultMethod->invokeArgs($stmt, $rowReferences); while (mysqli_stmt_fetch($stmt)) { $row = array(); foreach ($stmtRow as $key => $value) { $row[$key] = $value; } $queryResult[] = $row; } mysqli_stmt_free_result($stmt); } else { $queryResult[] = mysqli_stmt_affected_rows($stmt); } } else { $queryResult[] = FALSE; } $result[$queryKey] = $queryResult; } mysqli_stmt_close($stmt); } else { $app->logger->error("Error: Unable to connect to MySQL."); $app->logger->error("Debugging errno: " . mysqli_errno($link)); $app->logger->error("Debugging error: " . mysqli_error($link)); exit; } if ($multiQuery) { return $result; } else { return $result[0]; } }
/** * Returns $numItems rows starting from the $startIndex row from the * table. * * Add authorization or any logical checks for secure access to your data * * * * @return array */ public function getIntervalle_paged($startIndex, $numItems) { $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?"); $this->throwExceptionOnError(); mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->id, $row->idTimeline, $row->title, $row->description, $row->start, $row->end); while (mysqli_stmt_fetch($stmt)) { $row->start = new DateTime($row->start); $row->end = new DateTime($row->end); $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->id, $row->idTimeline, $row->title, $row->description, $row->start, $row->end); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; }
public function updateEmployee($item) { $stmt = mysqli_prepare($this->connection, "UPDATE employees SET\n\t\t\tfirstname=?,lastname=?,title=?,departmentid=?,officephone=?,cellphone=?, \t\n\t\t\temail=?,street=?,city=?,state=?,zipcode=?,office=?,photofile=?\n\t\t\tWHERE id=?"); $this->throwExceptionOnError(); mysqli_bind_param($stmt, 'sssisssssssssi', $item->firstname, $item->lastname, $item->title, $item->departmentid, $item->officephone, $item->cellphone, $item->email, $item->street, $item->city, $item->state, $item->zipcode, $item->office, $item->photofile, $item->id); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); mysqli_stmt_free_result($stmt); mysqli_close($this->connection); }
public static function closeCursor($sql) { if ($sql) { if (get_class($sql) == "mysqli_result") { mysqli_free_result($sql); } else { mysqli_stmt_free_result($sql); } } }
/** * Returns $numItems rows starting from the $startIndex row from the * table. * * Add authorization or any logical checks for secure access to your data * * * * @return array */ public function getScreenshot_paged($startIndex, $numItems) { $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?"); $this->throwExceptionOnError(); mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->id, $row->filename, $row->traceUri, $row->time); while (mysqli_stmt_fetch($stmt)) { $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->id, $row->filename, $row->traceUri, $row->time); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; }
/** * Free the result * * @return null */ function free_result() { if ($this->prepare_statement !== false) { mysqli_stmt_free_result($this->prepare_statement); $this->prepare_statement = FALSE; } }