downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  

<mysqli::set_local_infile_defaultmysqli->sqlstate>
Last updated: Thu, 26 Jun 2008

mysqli::set_local_infile_handler

mysqli_set_local_infile_handler

(PHP 5)

mysqli_set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command

Description

bool mysqli_set_local_infile_handler ( mysqli $link , callback $read_func )

Object oriented style (method)

mysqli
bool set_local_infile_handler ( mysqli $link , callback $read_func )

Set callback function for LOAD DATA LOCAL INFILE command

The callbacks task is to read input from the file specified in the LOAD DATA LOCAL INFILE and to reformat it into the format understood by LOAD DATA INFILE.

The returned data needs to match the format speficied in the LOAD DATA

Parameters

link

Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

read_func

A callback function or object method taking the following parameters:

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 occures you can store an error message in here

The callback function should return the number of characters stored in the buffer or a negative value if an error occured.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 Object oriented style

<?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";
  }
?>

Example #2 Procedural style

<?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";
  }
?>

The above example will output:

Input:
23,foo
42,bar

Output:
23,FOO
42,BAR


add a noteadd a note User Contributed Notes
Set callback function for LOAD DATA LOCAL INFILE command
There are no user contributed notes for this page.




<mysqli::set_local_infile_defaultmysqli->sqlstate>
Last updated: Thu, 26 Jun 2008
show source | credits | sitemap | contact | advertising | mirror sites
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://phpbuilder.com/
Last updated: Tue Nov 1 20:20:59 2005 EST
Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs