PHP mysqli: set_local_infile_handler() function
mysqli_set_local_infile_handler function / mysqli::set_local_infile_handler
The mysqli_set_local_infile_handler function / mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command.
Syntax:
Object oriented style
bool mysqli::set_local_infile_handler ( mysqli $link , callable $read_func )
Procedural style
bool mysqli_set_local_infile_handler ( mysqli $link , callable $read_func )
Parameter:
Name | Description | Required/Optional | |
---|---|---|---|
link | A link identifier returned by mysqli_connect() or mysqli_init() | Required for procedural style only and Optional for Object oriented style | |
read_func | A callback function or object method taking the following parameters: | Required | |
stream | A PHP stream associated with the SQL commands INFILE | ||
&buffer | A string buffer to store the rewritten input into. | ||
buflen | The maximum number of characters to be stored in the buffer | ||
&errormsg | If an error occurs you can store an error message in here |
Usage: Procedural style
mysqli_set_local_infile_handler(connection);
Parameter:
Name | Description | Required/Optional |
---|---|---|
connection | Specifies the MySQL connection to use | Required |
Return value:
Returns TRUE on success or FALSE on failure.
Version: PHP 5, PHP 7
Example of object oriented style:
<?php
$db = mysqli_init();
$db->real_connect("localhost","user1","datasoft123","hr");
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";
}
?>
Example of the Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($link));
/* change character set to utf8 */
if (!mysqli_set_charset($link, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($link));
exit();
} else {
printf("Current character set: %s\n", mysqli_character_set_name($link));
}
mysqli_close($link);
?>
Output:
Input: 23,foo 42,bar Output: 23,FOO 42,BAR
Example:
<?php
$con = mysqli_init();
$con->real_connect("localhost","user1","datasoft123","hr");
function Calling($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";
$con->set_local_infile_handler("Calling");
$con->query("LOAD DATA LOCAL INFILE 'MyInput.txt' INTO TABLE table1");
$con->set_local_infile_default();
$res = $con->query("SELECT * FROM table1");
echo "\nResult:\n";
while ($row = $res->fetch_assoc()) {
echo join(",", $row)."\n";
}
?>
Output:
Input:
See also
Previous: set_local_infile_default
Next: sqlstate
PHP: Tips of the Day
PHP: How to generate a random, unique, alphanumeric string for use in a secret link?
Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.
If you do not need it to be absolutely unique over time:
md5(uniqid(rand(), true))
Otherwise (given you have already determined a unique login for your user):
md5(uniqid($your_user_login, true))
Ref : https://bit.ly/31fd9wa
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook