mysqli::set_local_infile_handler
mysqli::set_local_infile_handler
mysqli_set_local_infile_handler
(PHP 5, PHP 7)
mysqli :: set_local_infile_handler - mysqli_set_local_infile_handler - 为LOAD DATA LOCAL INFILE 命令设置回调函数
描述
面向对象的风格
bool mysqli::set_local_infile_handler ( mysqli $link , callable $read_func )
程序风格
bool mysqli_set_local_infile_handler ( mysqli $link , callable $read_func )
为 LOAD DATA LOCAL INFILE 命令设置回调函数
回调任务是从 LOAD DATA LOCAL INFILE 中指定的文件读取输入,并将其重新格式化为 LOAD DATA INFILE 理解的格式。
返回的数据需要与 LOAD DATA 中指定的格式相匹配
参数
`link`
仅过程样式:由 mysqli_connect()或 mysqli_init()返回的链接标识符
read_func
采用以下参数的回调函数或对象方法:
stream
与 SQL 命令 INFILE 关联的 PHP 流
&buffer
将重写的输入存储到的字符串缓冲区
buflen
要存储在缓冲区中的最大字符数
&errormsg
如果发生错误,您可以在此处存储错误消息
buffer
如果发生错误,回调函数应返回存储在或为负值的字符数。
返回值
TRUE
成功或FALSE
失败时返回。
例子
Example #1 mysqli::set
_
local
_
infile
_
handler() example
面向对象的风格
<?php
$db = mysqli_init(
$db->real_connect("localhost","root","","test"
function callme($stream, &$buffer, $buflen, &$errmsg)
{
$buffer = fgets($stream
echo $buffer;
// convert to upper case and replace "," delimiter with [TAB]
$buffer = strtoupper(str_replace(",", "\t", $buffer)
return strlen($buffer
}
echo "Input:\n";
$db->set_local_infile_handler("callme"
$db->query("LOAD DATA LOCAL INFILE 'input.txt' INTO TABLE t1"
$db->set_local_infile_default(
$res = $db->query("SELECT * FROM t1"
echo "\nResult:\n";
while ($row = $res->fetch_assoc()) {
echo join(",", $row)."\n";
}
?>
程序风格
<?php
$db = mysqli_init(
mysqli_real_connect($db, "localhost","root","","test"
function callme($stream, &$buffer, $buflen, &$errmsg)
{
$buffer = fgets($stream
echo $buffer;
// convert to upper case and replace "," delimiter with [TAB]
$buffer = strtoupper(str_replace(",", "\t", $buffer)
return strlen($buffer
}
echo "Input:\n";
mysqli_set_local_infile_handler($db, "callme"
mysqli_query($db, "LOAD DATA LOCAL INFILE 'input.txt' INTO TABLE t1"
mysqli_set_local_infile_default($db
$res = mysqli_query($db, "SELECT * FROM t1"
echo "\nResult:\n";
while ($row = mysqli_fetch_assoc($res)) {
echo join(",", $row)."\n";
}
?>
上面的例子会输出:
Input:
23,foo
42,bar
Output:
23,FOO
42,BAR