📄 mysqli_stmt_bind_result.phpt
字号:
--TEST--mysqli_stmt_bind_result()--SKIPIF--<?php require_once('skipif.inc'); ?><?php require_once('skipifemb.inc'); ?>--FILE--<?php include "connect.inc"; $hint_str_or_unicode = ini_get("unicode.semantics")? "unicode":"string"; $tmp = NULL; $link = NULL; if (!is_null($tmp = @mysqli_stmt_bind_result())) printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); if (!is_null($tmp = @mysqli_stmt_bind_result($link))) printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); require('table.inc'); $stmt = mysqli_stmt_init($link); $id = null; $label = null; $foo = null; if (!is_null($tmp = mysqli_stmt_bind_result($stmt))) printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 1")) printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id))) printf("[005] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); if (true !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label))) printf("[006] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp); if (false !== ($tmp = mysqli_stmt_bind_result($stmt, $id, $label, $foo))) printf("[007] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); if (!mysqli_stmt_execute($stmt)) printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); while (mysqli_stmt_fetch($stmt)) { var_dump($id); var_dump($label); } mysqli_stmt_close($stmt); function func_mysqli_stmt_bind_result($link, $engine, $bind_type, $sql_type, $bind_value, $offset, $type_hint = null) { if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) { printf("[%04d] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link)); return false; } if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine))) { // don't bail - column type might not be supported by the server, ignore this return false; } if (!$stmt = mysqli_stmt_init($link)) { printf("[%04d] [%d] %s\n", $offset + 1, mysqli_errno($link), mysqli_error($link)); return false; } if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)")) { printf("[%04d] [%d] %s\n", $offset + 2, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); return false; } $id = null; if (!mysqli_stmt_bind_param($stmt, "i" . $bind_type, $id, $bind_value)) { printf("[%04d] [%d] %s\n", $offset + 3, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } for ($id = 1; $id < 4; $id++) { if (!mysqli_stmt_execute($stmt)) { printf("[%04d] [%d] %s\n", $offset + 3 + $id, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } } mysqli_stmt_close($stmt); $stmt = mysqli_stmt_init($link); if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test")) { printf("[%04d] [%d] %s\n", $offset + 7, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } if (!mysqli_stmt_execute($stmt)) { printf("[%04d] [%d] %s\n", $offset + 8, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } $result = mysqli_stmt_result_metadata($stmt); $bind_res = null; if (!mysqli_stmt_bind_result($stmt, $id, $bind_res)) { printf("[%04d] [%d] %s\n", $offset + 9, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } $num = 0; $fields = mysqli_fetch_fields($result); while (mysqli_stmt_fetch($stmt)) { if (!gettype($bind_res)=="unicode") { if ($bind_res !== $bind_value && (!$type_hint || ($type_hint !== gettype($bind_res)))) { printf("[%04d] [%d] Expecting %s/'%s' [type hint = %s], got %s/'%s'\n", $offset + 10, $num, gettype($bind_value), $bind_value, $type_hint, gettype($bind_res), $bind_res); mysqli_stmt_close($stmt); return false; } } $num++; } if ($num != 3) { printf("[%04d] [%d] %s, expecting 3 results, got only %d results\n", $offset + 11, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt), $num); mysqli_stmt_close($stmt); return false; } mysqli_stmt_close($stmt); return true; } function func_mysqli_stmt_bind_make_string($len) { $ret = ''; for ($i = 0; $i < $len; $i++) $ret .= chr(mt_rand(65, 90)); return $ret; } func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", -11, 20); func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT", NULL, 40); func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", 1, 60); func_mysqli_stmt_bind_result($link, $engine, "i", "TINYINT UNSIGNED", NULL, 80);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -