1--TEST--
2mysql_unbuffered_query()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10include_once("connect.inc");
11
12$tmp    = NULL;
13$link   = NULL;
14
15if (false !== ($tmp = @mysql_unbuffered_query($link)))
16	printf("[001] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
17
18require('table.inc');
19
20if (NULL !== ($tmp = @mysql_unbuffered_query("SELECT 1 AS a", $link, "foo")))
21	printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
22
23if (false !== ($tmp = mysql_unbuffered_query('THIS IS NOT SQL', $link)))
24	printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
25
26if (false !== ($tmp = mysql_unbuffered_query("SELECT 'this is sql but with backslash g'\g", $link)))
27	printf("[004] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
28
29if ((0 === mysql_errno($link)) || ('' == mysql_error($link)))
30	printf("[005] mysql_errno()/mysql_error should return some error\n");
31
32if (!$res = mysql_unbuffered_query("SELECT 'this is sql but with semicolon' AS valid ; ", $link))
33	printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
34
35var_dump(mysql_fetch_assoc($res));
36mysql_free_result($res);
37
38if (false !== ($res = mysql_unbuffered_query("SELECT 'this is sql but with semicolon' AS valid ; SHOW VARIABLES", $link)))
39	printf("[007] [%d] %s\n", mysql_errno($link), mysql_error($link));
40
41if (mysql_unbuffered_query('DROP PROCEDURE IF EXISTS p', $link)) {
42	// let's try to play with stored procedures
43	if (mysql_unbuffered_query('CREATE PROCEDURE p(OUT ver_param VARCHAR(25)) BEGIN SELECT VERSION() INTO ver_param; END;', $link)) {
44		$res = mysql_unbuffered_query('CALL p(@version)', $link);
45		$res = mysql_unbuffered_query('SELECT @version AS p_version', $link);
46		$tmp = mysql_fetch_assoc($res);
47		if (!isset($tmp['p_version']) || ('' == $tmp['p_version'])) {
48			printf("[008] Result seems wrong, dumping\n");
49			var_dump($tmp);
50		}
51		if ((version_compare(PHP_VERSION, '5.9.9', '>') == 1) && !is_unicode($tmp['p_version'])) {
52			printf("[009] Expecting unicode string, dumping\n");
53			var_dump($tmp);
54		}
55		mysql_free_result($res);
56	} else {
57		printf("[010] [%d] %s\n", mysql_errno($link), mysql_error($link));
58	}
59
60	mysql_unbuffered_query('DROP FUNCTION IF EXISTS f', $link);
61	if (mysql_unbuffered_query('CREATE FUNCTION f( ver_param VARCHAR(25)) RETURNS VARCHAR(25) DETERMINISTIC RETURN ver_param;', $link)) {
62		$res = mysql_unbuffered_query('SELECT f(VERSION()) AS f_version', $link);
63		$tmp = mysql_fetch_assoc($res);
64		if (!isset($tmp['f_version']) || ('' == $tmp['f_version'])) {
65			printf("[011] Result seems wrong, dumping\n");
66			var_dump($tmp);
67		}
68		if ((version_compare(PHP_VERSION, '5.9.9', '>') == 1) && !is_unicode($tmp['f_version'])) {
69			printf("[012] Expecting unicode string, dumping\n");
70			var_dump($tmp);
71		}
72		mysql_free_result($res);
73	} else {
74		printf("[012] [%d] %s\n", mysql_errno($link), mysql_error($link));
75	}
76}
77
78var_dump(mysql_unbuffered_query('INSERT INTO test(id) VALUES (100)', $link));
79var_dump($res = mysql_unbuffered_query('SELECT id FROM test', $link));
80var_dump(mysql_num_rows($res));
81
82mysql_close($link);
83
84if (false !== ($tmp = mysql_unbuffered_query("SELECT id FROM test", $link)))
85	printf("[010] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
86
87print "done!";
88?>
89--CLEAN--
90<?php
91require_once('connect.inc');
92
93// connect + select_db
94if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) {
95	printf("[clean] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
96 	  $host, $myhost, $user, $db, $port, $socket);
97}
98
99if (!mysql_query('DROP TABLE IF EXISTS test', $link)) {
100	printf("[clean] Failed to drop test table: [%d] %s\n", mysql_errno($link), mysql_error($link));
101}
102
103/* MySQL server may not support this - ignore errors */
104@mysql_query('DROP PROCEDURE IF EXISTS p', $link);
105@mysql_query('DROP FUNCTION IF EXISTS f', $link);
106
107mysql_close($link);
108?>
109--EXPECTF--
110array(1) {
111  [%u|b%"valid"]=>
112  %unicode|string%(30) "this is sql but with semicolon"
113}
114bool(true)
115resource(%d) of type (mysql result)
116int(0)
117
118Notice: mysql_close(): Function called without first fetching all rows from a previous unbuffered query in %s on line %d
119
120Warning: mysql_unbuffered_query(): %d is not a valid MySQL-Link resource in %s on line %d
121done!
122