File: mysqli_execute_query.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (97 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--TEST--
mysqli_execute_query()
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require 'table.inc';

if (!($tmp = mysqli_execute_query($link, "SELECT id, label FROM test ORDER BY id"))) {
    printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}

if (!is_a($tmp, mysqli_result::class)) {
    printf("[002] Expecting mysqli_result, got %s/%s\n", gettype($tmp), $tmp);
}

unset($tmp);

// procedural
if (!($tmp = mysqli_execute_query($link, "SELECT ? AS a, ? AS b, ? AS c", [42, "foo", null]))) {
    printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}

assert($tmp->fetch_assoc() === ['a' => '42', 'b' => 'foo', 'c' => null]);

// OO style
if (!($tmp = $link->execute_query("SELECT ? AS a, ? AS b, ? AS c", [42, "foo", null]))) {
    printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}

assert($tmp->fetch_assoc() === ['a' => '42', 'b' => 'foo', 'c' => null]);

// prepare error
if (false === $link->execute_query("some random gibberish", [1, "foo"])) {
    printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}

// stmt error - duplicate key
if (!$link->execute_query("INSERT INTO test(id, label) VALUES (?, ?)", [1, "foo"])) {
    printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}

// successful update returns true
if (!($tmp = $link->execute_query("UPDATE test SET label=? WHERE id=?", ["baz", 1]))) {
    printf("[007] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
}
if ($link->affected_rows <= 0) {
    printf("[008] Expecting positive non-zero integer for affected_rows, got %s/%s\n", gettype($link->affected_rows), $link->affected_rows);
}

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// check if value error is properly reported
try {
    $link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo', 42]);
} catch (ValueError $e) {
    echo '[009] '.$e->getMessage()."\n";
}
try {
    $link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo' => 42]);
} catch (ValueError $e) {
    echo '[010] '.$e->getMessage()."\n";
}

// check if insert_id is copied
$link->execute_query("ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT");
$link->execute_query("INSERT INTO test(label) VALUES (?)", ["foo"]);
if ($link->insert_id <= 0) {
    printf("[011] Expecting positive non-zero integer for insert_id, got %s/%s\n", gettype($link->insert_id), $link->insert_id);
}

// bad index
mysqli_report(MYSQLI_REPORT_ALL);
try {
    $link->execute_query("SELECT id FROM test WHERE label = ?", ["foo"]);
} catch (mysqli_sql_exception $e) {
    echo '[012] '.$e->getMessage()."\n";
}

print "done!";
?>
--CLEAN--
<?php
require_once 'clean_table.inc';
?>
--EXPECTF--
[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
[006] [1062] Duplicate entry '1' for key '%s'
[009] mysqli::execute_query(): Argument #2 ($params) must consist of exactly 3 elements, 2 present
[010] mysqli::execute_query(): Argument #2 ($params) must be a list array
[012] No index used in query/prepared statement SELECT id FROM test WHERE label = ?
done!