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