/** * Execute a prepared query statement helper method. * * @param mixed $result_class string which specifies which result class to use * @param mixed $result_wrap_class string which specifies which class to wrap results in * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure * @access private */ function &_execute($result_class = true, $result_wrap_class = false) { if (is_null($this->statement)) { $result =& parent::_execute($result_class, $result_wrap_class); return $result; } $this->db->last_query = $this->query; $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values)); if ($this->db->getOption('disable_query')) { $result = $this->is_manip ? 0 : null; return $result; } $connection = $this->db->getConnection(); if (PEAR::isError($connection)) { return $connection; } if (!is_object($this->statement)) { $query = 'EXECUTE ' . $this->statement; } if (!empty($this->positions)) { $parameters = array(0 => $this->statement, 1 => ''); $lobs = array(); $i = 0; foreach ($this->positions as $parameter) { if (!array_key_exists($parameter, $this->values)) { return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 'Unable to bind to missing placeholder: ' . $parameter, __FUNCTION__); } $value = $this->values[$parameter]; $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null; if (!is_object($this->statement)) { if (is_resource($value) || $type == 'clob' || $type == 'blob') { if (!is_resource($value) && preg_match('/^(\\w+:\\/\\/)(.*)$/', $value, $match)) { if ($match[1] == 'file://') { $value = $match[2]; } $value = @fopen($value, 'r'); $close = true; } if (is_resource($value)) { $data = ''; while (!@feof($value)) { $data .= @fread($value, $this->db->options['lob_buffer_length']); } if ($close) { @fclose($value); } $value = $data; } } $quoted = $this->db->quote($value, $type); if (PEAR::isError($quoted)) { return $quoted; } $param_query = 'SET @' . $parameter . ' = ' . $quoted; $result = $this->db->_doQuery($param_query, true, $connection); if (PEAR::isError($result)) { return $result; } } else { if (is_resource($value) || $type == 'clob' || $type == 'blob') { $parameters[] = null; $parameters[1] .= 'b'; $lobs[$i] = $parameter; } else { $parameters[] = $this->db->quote($value, $type, false); $parameters[1] .= $this->db->datatype->mapPrepareDatatype($type); } ++$i; } } if (!is_object($this->statement)) { $query .= ' USING @' . implode(', @', array_values($this->positions)); } else { $result = @call_user_func_array('mysqli_stmt_bind_param', $parameters); if ($result === false) { $err =& $this->db->raiseError(null, null, null, 'Unable to bind parameters', __FUNCTION__); return $err; } foreach ($lobs as $i => $parameter) { $value = $this->values[$parameter]; $close = false; if (!is_resource($value)) { $close = true; if (preg_match('/^(\\w+:\\/\\/)(.*)$/', $value, $match)) { if ($match[1] == 'file://') { $value = $match[2]; } $value = @fopen($value, 'r'); } else { $fp = @tmpfile(); @fwrite($fp, $value); @rewind($fp); $value = $fp; } } while (!@feof($value)) { $data = @fread($value, $this->db->options['lob_buffer_length']); @mysqli_stmt_send_long_data($this->statement, $i, $data); } if ($close) { @fclose($value); } } } } if (!is_object($this->statement)) { $result = $this->db->_doQuery($query, $this->is_manip, $connection); if (PEAR::isError($result)) { return $result; } if ($this->is_manip) { $affected_rows = $this->db->_affectedRows($connection, $result); return $affected_rows; } $result =& $this->db->_wrapResult($result, $this->result_types, $result_class, $result_wrap_class, $this->limit, $this->offset); } else { if (!@mysqli_stmt_execute($this->statement)) { $err =& $this->db->raiseError(null, null, null, 'Unable to execute statement', __FUNCTION__); return $err; } if ($this->is_manip) { $affected_rows = @mysqli_stmt_affected_rows($this->statement); return $affected_rows; } if ($this->db->options['result_buffering']) { @mysqli_stmt_store_result($this->statement); } $result =& $this->db->_wrapResult($this->statement, $this->result_types, $result_class, $result_wrap_class, $this->limit, $this->offset); } $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result)); return $result; }
<?php require_once "connect.inc"; /*** test mysqli_connect 127.0.0.1 ***/ $link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket); mysqli_select_db($link, $db); mysqli_query($link, "SET sql_mode=''"); mysqli_query($link, "DROP TABLE IF EXISTS test_026_table_1"); mysqli_query($link, "CREATE TABLE test_026_table_1(c1 varchar(10), c2 text)"); $stmt = mysqli_prepare($link, "INSERT INTO test_026_table_1 VALUES (?,?)"); mysqli_stmt_bind_param($stmt, "sb", $c1, $c2); $c1 = "Hello World"; mysqli_stmt_send_long_data($stmt, 1, "This is the first sentence."); mysqli_stmt_send_long_data($stmt, 1, " And this is the second sentence."); mysqli_stmt_send_long_data($stmt, 1, " And finally this is the last sentence."); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); $stmt = mysqli_prepare($link, "SELECT * FROM test_026_table_1"); mysqli_stmt_bind_result($stmt, $d1, $d2); mysqli_stmt_execute($stmt); mysqli_stmt_fetch($stmt); $test = array($d1, $d2); var_dump($test); mysqli_stmt_close($stmt); mysqli_query($link, "DROP TABLE IF EXISTS test_026_table_1"); mysqli_close($link); print "done!"; require_once "connect.inc"; if (!($link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))) { printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); }
if (0 === ($max_allowed_packet = (int) $row['Value'])) { printf("[008] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n"); } // let's ignore upper limits for LONGBLOB (2^32) ... // maximum packet size up to which we test is 10M $tmp = ''; $blob = ''; for ($i = 0; $i < 1024; $i++) { $tmp .= 'a'; } $limit = min(floor($max_allowed_packet / 1024 / 2), 10240); for ($i = 0; $i < $limit; $i++) { $blob .= $tmp; } assert(strlen($blob) <= $max_allowed_packet); if (true != ($tmp = mysqli_stmt_send_long_data($stmt, 1, $blob))) { printf("[009] Expecting boolean/true, got %s/%s. [%d] %s\n", gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } $id = 1; if (true !== mysqli_stmt_execute($stmt)) { printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } /* TODO - we skip this part of the test for now, because of bugs.mysql.com/26824 if (floor($max_allowed_packet / 1024 / 2) <= 10240) { // test with a blob smaller than 10M allows us to test // for too long packages without wasting too much memory $limit = $max_allowed_packet - strlen($blob) + 1; $blob2 = $blob; for ($i = 0; $i < $limit; $i++) $blob2 .= 'b';
if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) { printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } if (!mysqli_query($link, "CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT, label BLOB, PRIMARY KEY(id))")) { printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(label) VALUES (?)")) { printf("[013] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } $label = null; if (!mysqli_stmt_bind_param($stmt, "b", $label)) { printf("[014] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } $label = 'abc'; for ($i = 0; $i < 10; $i++) { if (!mysqli_stmt_send_long_data($stmt, 0, $label)) { printf("[015 - %d] [%d] %s\n", $i, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } } if (!mysqli_stmt_reset($stmt)) { printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } if (!mysqli_stmt_execute($stmt)) { printf("[017] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } if (!($res = mysqli_query($link, "SELECT label FROM test"))) { printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } if (!($row = mysqli_fetch_assoc($res))) { printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); }
$id = 1; if (true !== mysqli_stmt_execute($stmt)) { printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); if (!($res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id"))) { printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } if (1 != ($tmp = mysqli_num_rows($res))) { printf("[018] Expecting 1 rows, mysqli_num_rows() reports %d rows. [%d] %s\n", $tmp, mysqli_errno($link), mysqli_error($link)); } if (!($row = mysqli_fetch_assoc($res))) { printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } mysqli_free_result($res); if (empty($row['id']) || empty($row['label']) || $row['id'] != 1) { printf("[020] Record seems to be incomplete\n"); } if ($blob != $row['label']) { printf("[021] Blob value has not been stored properly!\n"); } if (NULL !== ($tmp = @mysqli_stmt_send_long_data($stmt, ''))) { printf("[022] Expecting NULL, got %s/%s\n"); } /* Check that the function alias exists. It's a deprecated function, but we have not announce the removal so far, therefore we need to check for it */ if (!is_null($tmp = @mysqli_stmt_send_long_data())) { printf("[023] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } mysqli_close($link); print "done!";
private function send_lobs() { foreach ($this->lobs as $k => &$v) { if (is_resource($v)) { while (!feof($v)) { if (!mysqli_stmt_send_long_data($this->_result, $k, fread($v, 8192))) { return false; } } } else { if (!mysqli_stmt_send_long_data($this->_result, $k, $v)) { return false; } } } return true; }