예제 #1
0
 public function seek($row_number)
 {
     return \mysqli_stmt_data_seek($this->res, $row_number);
 }
if (!mysqli_stmt_fetch($stmt)) {
    printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
var_dump($id);
if (!is_null($tmp = mysqli_stmt_data_seek($stmt, 0))) {
    printf("[011] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
if (!mysqli_stmt_fetch($stmt)) {
    printf("[012] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
var_dump($id);
if (!is_null($tmp = mysqli_stmt_data_seek($stmt, mysqli_stmt_num_rows($stmt) + 100))) {
    printf("[013] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
if (mysqli_stmt_fetch($stmt)) {
    printf("[014] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
var_dump($id);
if (false !== ($tmp = mysqli_stmt_data_seek($stmt, -1))) {
    printf("[015] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
if (mysqli_stmt_fetch($stmt)) {
    printf("[016] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
}
var_dump($id);
mysqli_stmt_close($stmt);
if (NULL !== ($tmp = mysqli_stmt_data_seek($stmt, 0))) {
    printf("[017] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
}
mysqli_close($link);
print "done!";
예제 #3
0
 public function executeQuery($sql, $argv = NULL, $pagesize = 0, $pageno = 0)
 {
     if ($pagesize < 0) {
         $pagesize = 0;
     }
     if ($pageno < 0) {
         $pageno = 0;
     }
     // 校验参数有效性
     $lowstr = strtolower($sql);
     if (!(strtolower(substr($lowstr, 0, 6)) === "select")) {
         echo "Invalid query SQL statement.";
     }
     $pos = strpos($lowstr, "from");
     if (!strpos($lowstr, "from")) {
         echo "Invalid query SQL statement.";
     }
     $colstr = substr($lowstr, 6, $pos - 6);
     // 计算查询语句输出的字段个数
     $pos = 0;
     $colcount = 0;
     while (!($pos === false)) {
         $pos = strpos($colstr, ",", $pos + 1);
         $colcount++;
     }
     // 创建数据库连接(如果需要)
     $connected = $this->connected();
     $conn = $connected ? $this->conn : $this->connect(FALSE);
     // 将默认字符集设置为utf8
     mysqli_query($conn, "set names 'utf8'");
     mysqli_query($conn, "set character set 'utf8'");
     // 查询
     $allrow = array();
     $stmt = mysqli_prepare($conn, $sql);
     if (mysqli_errno($conn)) {
         $errno = mysqli_errno($conn);
         $error = "MYSQL ERROR #" . $errno . " : " . mysqli_error($conn);
         echo $error;
     }
     // 根据参数的个数动态生成参数绑定语句
     if (isset($argv) && count($argv) > 0) {
         $bind_param_cmd = "mysqli_stmt_bind_param(\$stmt, ";
         $paramstr = "";
         $bindstr = "";
         $holdstr = "";
         $i = 0;
         foreach ($argv as $arg) {
             $paramstr .= "\$invar{$i}, ";
             $bindstr .= "\$invar{$i} = \$argv[{$i}]; ";
             $holdstr .= "s";
             $i++;
         }
         $bind_param_cmd = "mysqli_stmt_bind_param(\$stmt, \"{$holdstr}\", " . substr($paramstr, 0, strlen($paramstr) - 2) . "); ";
         $bind_param_cmd .= $bindstr;
         eval($bind_param_cmd);
     }
     // 执行SQL语句
     mysqli_stmt_execute($stmt);
     if (mysqli_stmt_errno($stmt)) {
         $errno = mysqli_stmt_errno($stmt);
         $error = "MYSQL ERROR #" . $errno . " : " . mysqli_stmt_error($stmt);
         echo $error;
     }
     // 根据查询的字段数动态生成结果绑定语句
     // 语句形如:mysqli_stmt_bind_result($stmt, $ret1, $ret2, $ret3, ...);
     $bind_result_cmd = "mysqli_stmt_bind_result(\$stmt, ";
     for ($i = 0; $i < $colcount; $i++) {
         $bind_result_cmd .= "\$outvar{$i}, ";
     }
     $bind_result_cmd = substr($bind_result_cmd, 0, strlen($bind_result_cmd) - 2) . ");";
     eval($bind_result_cmd);
     // 根据查询的字段数动态生成结果读取语句
     // 语句形如:$row = array($ret1, $ret2, $ret3, ...);
     $fetch_data_cmd = "\$row = array(";
     for ($i = 0; $i < $colcount; $i++) {
         $fetch_data_cmd .= "\$outvar{$i}, ";
     }
     $fetch_data_cmd = substr($fetch_data_cmd, 0, strlen($fetch_data_cmd) - 2) . ");";
     mysqli_stmt_store_result($stmt);
     // 分页检索处理
     if ($pagesize > 0) {
         mysqli_stmt_data_seek($stmt, $pagesize * $pageno);
     }
     $cnt = 1;
     while (mysqli_stmt_fetch($stmt)) {
         eval($fetch_data_cmd);
         $allrow[] = $row;
         if ($pagesize > 0) {
             if (++$cnt > $pagesize) {
                 break;
             }
         }
     }
     mysqli_stmt_close($stmt);
     // 关闭数据库连接(如果需要)
     if (!$connected) {
         $this->disconnect($conn);
     }
     return $allrow;
 }
예제 #4
0
         break 4;
     }
     $id = $label = null;
     if (!mysqli_stmt_bind_result($stmt, $id, $label)) {
         $errors[] = sprintf("stmt_bind_result() failed (converted code: %s)\n", $flag_original_code ? 'no' : 'yes');
         break 4;
     }
     if (!mysqli_stmt_store_result($stmt)) {
         $errors[] = sprintf("stmt_store_result() failed (converted code: %s)\n", $flag_original_code ? 'no' : 'yes');
         break 4;
     }
     $times[$num_rows . ' rows: ' . $num_runs . 'x ' . $type . ' overall'] = microtime(true);
     for ($i = 0; $i < $num_runs; $i++) {
         $pos = $num_runs - $i - 1;
         $start = microtime(true);
         mysqli_stmt_data_seek($stmt, $pos);
         $times[$num_rows . ' rows: ' . $num_runs . 'x ' . $type . ' data_seek()'] += microtime(true) - $start;
         if (!mysqli_stmt_fetch($stmt)) {
             $errors[] = sprintf("fetch() for %dth of %d rows failed (%s) (converted code: %s): [%d] %s\n", $pos, $num_rows, $type, $flag_original_code ? 'no' : 'yes', mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
             break 5;
         }
         if ($id != $pos) {
             $errors[] = sprintf("seek() + fetch() for %dth of %d rows did not work (%s) (converted code: %s): [%d] %s\n", $pos, $num_rows, $type, $flag_original_code ? 'no' : 'yes', mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
             var_dump($id);
             var_dump($pos);
             break 5;
         }
     }
     $times[$num_rows . ' rows: ' . $num_runs . 'x ' . $type . ' overall'] = microtime(true) - $times[$num_rows . ' rows: ' . $num_runs . 'x ' . $type . ' overall'];
     mysqli_stmt_close($stmt);
 }
예제 #5
0
 public function executeQueryA($sql, $argv = NULL, $pagesize = 0, $pageno = 0)
 {
     //Logger::trace("MysqlDao.executeQuery executed", LOG_LEVEL_NOTICE);
     if ($pagesize < 0) {
         $pagesize = 0;
     }
     if ($pageno < 0) {
         $pageno = 0;
     }
     $lowstr = strtolower($sql);
     if (!(strtolower(substr($lowstr, 0, 6)) === "select")) {
         throw new DaoException("Invalid query SQL statement.");
     }
     $pos = strpos($lowstr, "from");
     if (!strpos($lowstr, "from")) {
         //Logger::trace("Invalid query SQL statement.", LOG_LEVEL_ERROR);
         //Logger::debug("sql = $sql, argv = $argv");
         throw new DaoException("Invalid query SQL statement.");
     }
     $colstr = substr($lowstr, 6, $pos - 6);
     $pos = 0;
     $colcount = 0;
     while (!($pos === false)) {
         $pos = strpos($colstr, ",", $pos + 1);
         $colcount++;
     }
     $connected = $this->connected();
     $conn = $connected ? $this->conn : $this->connect(FALSE);
     mysqli_query($conn, "set names 'utf8'");
     $allrow = array();
     $stmt = mysqli_prepare($conn, $sql);
     if (mysqli_errno($conn)) {
         $errno = mysqli_errno($conn);
         $error = "MYSQL ERROR #" . $errno . " : " . mysqli_error($conn);
         throw new DaoException($error, $errno);
     }
     //Logger::trace("sql = " . $sql, LOG_LEVEL_VERBOSE);
     if (isset($argv) && count($argv) > 0) {
         $bind_param_cmd = "mysqli_stmt_bind_param(\$stmt, ";
         $paramstr = "";
         $bindstr = "";
         $holdstr = "";
         $i = 0;
         foreach ($argv as $arg) {
             $paramstr .= "\$invar{$i}, ";
             $bindstr .= "\$invar{$i} = \$argv[{$i}]; ";
             $holdstr .= "s";
             $i++;
         }
         $bind_param_cmd = "mysqli_stmt_bind_param(\$stmt, \"{$holdstr}\", " . substr($paramstr, 0, strlen($paramstr) - 2) . "); ";
         $bind_param_cmd .= $bindstr;
         //Logger::trace("bind parameter: " . $bind_param_cmd, LOG_LEVEL_VERBOSE);
         eval($bind_param_cmd);
     }
     mysqli_stmt_execute($stmt);
     if (mysqli_stmt_errno($stmt)) {
         $errno = mysqli_stmt_errno($stmt);
         $error = "MYSQL ERROR #" . $errno . " : " . mysqli_stmt_error($stmt);
         //Logger::trace($error, LOG_LEVEL_ERROR);
         //Logger::debug("sql = $sql ". ($argv));
         throw new DaoException($error, $errno);
     }
     //��ý���Ԫ��� //
     /*&�����   ��j���� �Խ����ֶ�Ϊ����*/
     $data = mysqli_stmt_result_metadata($stmt);
     $fields = array();
     $out = array();
     $fields[0] = $stmt;
     $count = 1;
     while ($field = mysqli_fetch_field($data)) {
         $fields[$count] =& $out[$field->name];
         $count++;
     }
     call_user_func_array(mysqli_stmt_bind_result, $fields);
     //����
     mysqli_stmt_store_result($stmt);
     //��ҳ
     if ($pagesize > 0) {
         mysqli_stmt_data_seek($stmt, $pagesize * $pageno);
     }
     $cnt = 1;
     while (mysqli_stmt_fetch($stmt)) {
         /*���$row���ø�ֵֻ�����һ���¼������
         			�������ѭ�������ݵĸ�ֵ
         		*/
         foreach ($out as $key => $value) {
             $row_tmb[$key] = $value;
         }
         $allrow[] = $row_tmb;
         if ($pagesize > 0) {
             if (++$cnt > $pagesize) {
                 break;
             }
         }
     }
     mysqli_stmt_close($stmt);
     if (!$connected) {
         $this->disconnect($conn);
     }
     return $allrow;
 }