|
| 1 | +--TEST-- |
| 2 | +mysqli_stmt_execute() - bind in execute |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +require_once 'skipif.inc'; |
| 6 | +require_once 'skipifconnectfailure.inc'; |
| 7 | +if (!stristr(mysqli_get_client_info(), 'mysqlnd')) { |
| 8 | + die("skip: only available in mysqlnd"); |
| 9 | +} |
| 10 | +?> |
| 11 | +--FILE-- |
| 12 | +<?php |
| 13 | +require_once "connect.inc"; |
| 14 | + |
| 15 | +require 'table.inc'; |
| 16 | + |
| 17 | +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); |
| 18 | + |
| 19 | +// first, control case |
| 20 | +$id = 1; |
| 21 | +$abc = 'abc'; |
| 22 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 23 | +$stmt->bind_param('sss', ...[&$abc, 42, $id]); |
| 24 | +$stmt->execute(); |
| 25 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>'abc', 'num' => '42']); |
| 26 | +$stmt = null; |
| 27 | + |
| 28 | +// 1. same as the control case, but skipping the middle-man (bind_param) |
| 29 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 30 | +$stmt->execute([&$abc, 42, $id]); |
| 31 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>'abc', 'num' => '42']); |
| 32 | +$stmt = null; |
| 33 | + |
| 34 | +// 2. param number has to match - missing 1 parameter |
| 35 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 36 | +try { |
| 37 | + $stmt->execute([&$abc, 42]); |
| 38 | +} catch (ValueError $e) { |
| 39 | + echo '[001] '.$e->getMessage()."\n"; |
| 40 | +} |
| 41 | +$stmt = null; |
| 42 | + |
| 43 | +// 3. Too many parameters |
| 44 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 45 | +try { |
| 46 | + $stmt->execute([&$abc, null, $id, 24]); |
| 47 | +} catch (ValueError $e) { |
| 48 | + echo '[002] '.$e->getMessage()."\n"; |
| 49 | +} |
| 50 | +$stmt = null; |
| 51 | + |
| 52 | +// 4. param number has to match - missing all parameters |
| 53 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 54 | +try { |
| 55 | + $stmt->execute([]); |
| 56 | +} catch (ValueError $e) { |
| 57 | + echo '[003] '.$e->getMessage()."\n"; |
| 58 | +} |
| 59 | +$stmt = null; |
| 60 | + |
| 61 | +// 5. param number has to match - missing argument to execute() |
| 62 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 63 | +try { |
| 64 | + $stmt->execute(); |
| 65 | +} catch (mysqli_sql_exception $e) { |
| 66 | + echo '[004] '.$e->getMessage()."\n"; |
| 67 | +} |
| 68 | +$stmt = null; |
| 69 | + |
| 70 | +// 6. wrong argument to execute() |
| 71 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 72 | +try { |
| 73 | + $stmt->execute(42); |
| 74 | +} catch (TypeError $e) { |
| 75 | + echo '[005] '.$e->getMessage()."\n"; |
| 76 | +} |
| 77 | +$stmt = null; |
| 78 | + |
| 79 | +// 7. objects are not arrays and are not accepted |
| 80 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 81 | +try { |
| 82 | + $stmt->execute((object)[&$abc, 42, $id]); |
| 83 | +} catch (TypeError $e) { |
| 84 | + echo '[006] '.$e->getMessage()."\n"; |
| 85 | +} |
| 86 | +$stmt = null; |
| 87 | + |
| 88 | +// 8. arrays by reference work too |
| 89 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 90 | +$arr = [&$abc, 42, $id]; |
| 91 | +$arr2 = &$arr; |
| 92 | +$stmt->execute($arr2); |
| 93 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>'abc', 'num' => '42']); |
| 94 | +$stmt = null; |
| 95 | + |
| 96 | +// 9. no placeholders in statement. nothing to bind in an empty array |
| 97 | +$stmt = $link->prepare('SELECT label FROM test WHERE id=1'); |
| 98 | +$stmt->execute([]); |
| 99 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a']); |
| 100 | +$stmt = null; |
| 101 | + |
| 102 | +// 10. once bound the values are persisted. Just like in PDO |
| 103 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 104 | +$stmt->execute(['abc', 42, $id]); |
| 105 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>'abc', 'num' => '42']); |
| 106 | +$stmt->execute(); // no argument here. Values are already bound |
| 107 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>'abc', 'num' => '42']); |
| 108 | +try { |
| 109 | + $stmt->execute([]); // no params here. PDO doesn't throw an error, but mysqli does |
| 110 | +} catch (ValueError $e) { |
| 111 | + echo '[007] '.$e->getMessage()."\n"; |
| 112 | +} |
| 113 | +$stmt = null; |
| 114 | + |
| 115 | +// 11. mixing binding styles not possible. Also, NULL should stay NULL when bound as string |
| 116 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 117 | +$stmt->bind_param('sss', ...['abc', 42, null]); |
| 118 | +$stmt->execute([null, null, $id]); |
| 119 | +assert($stmt->get_result()->fetch_assoc() === ['label'=>'a', 'anon'=>null, 'num' => null]); |
| 120 | +$stmt = null; |
| 121 | + |
| 122 | +// 12. Only list arrays are allowed |
| 123 | +$stmt = $link->prepare('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?'); |
| 124 | +try { |
| 125 | + $stmt->execute(['A'=>'abc', 2=>42, null=>$id]); |
| 126 | +} catch (ValueError $e) { |
| 127 | + echo '[008] '.$e->getMessage()."\n"; |
| 128 | +} |
| 129 | +$stmt = null; |
| 130 | + |
| 131 | + |
| 132 | +mysqli_close($link); |
| 133 | +?> |
| 134 | +--CLEAN-- |
| 135 | +<?php |
| 136 | +require_once "clean_table.inc"; |
| 137 | +?> |
| 138 | +--EXPECT-- |
| 139 | +[001] mysqli_stmt::execute(): Argument #1 ($params) must consist of exactly 3 elements, 2 present |
| 140 | +[002] mysqli_stmt::execute(): Argument #1 ($params) must consist of exactly 3 elements, 4 present |
| 141 | +[003] mysqli_stmt::execute(): Argument #1 ($params) must consist of exactly 3 elements, 0 present |
| 142 | +[004] No data supplied for parameters in prepared statement |
| 143 | +[005] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, int given |
| 144 | +[006] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, stdClass given |
| 145 | +[007] mysqli_stmt::execute(): Argument #1 ($params) must consist of exactly 3 elements, 0 present |
| 146 | +[008] mysqli_stmt::execute(): Argument #1 ($params) must be a list array |
0 commit comments