1--TEST--
2mysqli_execute_query()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11
12require 'table.inc';
13
14if (!($tmp = mysqli_execute_query($link, "SELECT id, label FROM test ORDER BY id"))) {
15    printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
16}
17
18if (!is_a($tmp, mysqli_result::class)) {
19    printf("[002] Expecting mysqli_result, got %s/%s\n", gettype($tmp), $tmp);
20}
21
22unset($tmp);
23
24// procedural
25if (!($tmp = mysqli_execute_query($link, "SELECT ? AS a, ? AS b, ? AS c", [42, "foo", null]))) {
26    printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27}
28
29assert($tmp->fetch_assoc() === ['a' => '42', 'b' => 'foo', 'c' => null]);
30
31// OO style
32if (!($tmp = $link->execute_query("SELECT ? AS a, ? AS b, ? AS c", [42, "foo", null]))) {
33    printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
34}
35
36assert($tmp->fetch_assoc() === ['a' => '42', 'b' => 'foo', 'c' => null]);
37
38// prepare error
39if (false === $link->execute_query("some random gibberish", [1, "foo"])) {
40    printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
41}
42
43// stmt error - duplicate key
44if (!$link->execute_query("INSERT INTO test(id, label) VALUES (?, ?)", [1, "foo"])) {
45    printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
46}
47
48// successful update returns true
49if (!($tmp = $link->execute_query("UPDATE test SET label=? WHERE id=?", ["baz", 1]))) {
50    printf("[007] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
51}
52if ($link->affected_rows <= 0) {
53    printf("[008] Expecting positive non-zero integer for affected_rows, got %s/%s\n", gettype($link->affected_rows), $link->affected_rows);
54}
55
56mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
57
58// check if value error is properly reported
59try {
60    $link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo', 42]);
61} catch (ValueError $e) {
62    echo '[009] '.$e->getMessage()."\n";
63}
64try {
65    $link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo' => 42]);
66} catch (ValueError $e) {
67    echo '[010] '.$e->getMessage()."\n";
68}
69
70// check if insert_id is copied
71$link->execute_query("ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT");
72$link->execute_query("INSERT INTO test(label) VALUES (?)", ["foo"]);
73if ($link->insert_id <= 0) {
74    printf("[011] Expecting positive non-zero integer for insert_id, got %s/%s\n", gettype($link->insert_id), $link->insert_id);
75}
76
77// bad index
78mysqli_report(MYSQLI_REPORT_ALL);
79try {
80    $link->execute_query("SELECT id FROM test WHERE label = ?", ["foo"]);
81} catch (mysqli_sql_exception $e) {
82    echo '[012] '.$e->getMessage()."\n";
83}
84
85print "done!";
86?>
87--CLEAN--
88<?php
89require_once 'clean_table.inc';
90?>
91--EXPECTF--
92[005] [1064] You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'some random gibberish' at line 1
93[006] [1062] Duplicate entry '1' for key '%s'
94[009] mysqli::execute_query(): Argument #2 ($params) must consist of exactly 3 elements, 2 present
95[010] mysqli::execute_query(): Argument #2 ($params) must be a list array
96[012] No index used in query/prepared statement SELECT id FROM test WHERE label = ?
97done!
98