xref: /PHP-7.2/ext/mysqli/tests/061.phpt (revision 2eaabf06)
1--TEST--
2local infile handler
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7if (!function_exists('mysqli_set_local_infile_handler'))
8	die("skip - function not available.");
9
10$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
11if (!$link)
12	die(sprintf("skip Can't connect [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
13
14include_once("local_infile_tools.inc");
15if ($msg = check_local_infile_support($link, $engine))
16	die(sprintf("skip %s, [%d] %s", $msg, $link->errno, $link->error));
17
18mysqli_close($link);
19?>
20--INI--
21mysqli.allow_local_infile=1
22--FILE--
23<?php
24	require_once("connect.inc");
25
26	function my_read($fp, &$buffer, $buflen, &$error) {
27		$buffer = strrev(fread($fp, $buflen));
28		return(strlen($buffer));
29	}
30
31	/*** test mysqli_connect 127.0.0.1 ***/
32	$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
33
34	/* create temporary file */
35	$filename = dirname(__FILE__) . "061.csv";
36	$fp = fopen($filename, "w");
37	fwrite($fp, "foo;bar");
38	fclose($fp);
39
40	if (!mysqli_query($link,"DROP TABLE IF EXISTS t_061"))
41		printf("Cannot drop table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
42	if (!mysqli_query($link,"CREATE TABLE t_061 (c1 varchar(10), c2 varchar(10))"))
43		printf("Cannot create table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
44
45	if (!mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE t_061 FIELDS TERMINATED BY ';'", mysqli_real_escape_string($link, $filename))))
46		printf("Cannot load data: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47
48	mysqli_set_local_infile_handler($link, "my_read");
49	if (!mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE t_061 FIELDS TERMINATED BY ';'", mysqli_real_escape_string($link, $filename))))
50		printf("Cannot load data using infile handler: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
51
52	if ($result = mysqli_query($link, "SELECT c1,c2 FROM t_061")) {
53		while (($row = mysqli_fetch_row($result))) {
54			printf("%s-%s\n", $row[0], $row[1]);
55			printf("%s-%s\n", gettype($row[0]), gettype($row[1]));
56		}
57		mysqli_free_result($result);
58	}
59
60	mysqli_close($link);
61	unlink($filename);
62	print "done!";
63?>
64--CLEAN--
65<?php
66require_once("connect.inc");
67if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
68   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
69
70if (!mysqli_query($link, "DROP TABLE IF EXISTS t_061"))
71	printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
72
73mysqli_close($link);
74?>
75--EXPECTF--
76foo-bar
77string-string
78rab-oof
79string-string
80done!
81