douhuan6065 2012-04-13 18:13
浏览 173
已采纳

使用MySQL LOAD_FILE()将XML添加到列

I want to load an xml file into a table column with MySQL. How do I do this with an INSERT statement instead of an UPDATE statement?

This code does an INSERT to create a new row, gets the id for the last inserted row, then attempts to do the update to load the xml file. But it doesn't update the row with the xml info.

I would like to know what to fix to put the xml in the database column. I also want to streamline the code and do the LOAD_FILE in one INSERT statement.

MySQL database table structure

Field         Datatype       Attributes            Extra

userid        INT(6)         unsigned              auto_increment
user_events   LONGTEXT

PHP code to add xml file

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$con = mysql_connect("hostname","adminuser","adminpassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test_db", $con);

$qinsert = "INSERT INTO load_xml (`user_events`) VALUES ('user events')";
$result_insert = mysql_query($qinsert);

$qtext = "SELECT * FROM load_xml";
$result = mysql_query($qtext);
$numrows = mysql_num_rows ( $result );

while ($row = mysql_fetch_row($result)) {
    echo 'user id '.$row[0].' user_events '.$row[1].'<br/>';
}

$insert_id = "SELECT LAST_INSERT_ID()";
$rin_id = mysql_query($insert_id);
$row = mysql_fetch_row($rin_id);
$userid = $row[0];
echo 'last added id '.$userid.'<br/>';

$update_xml = "UPDATE load_xml SET user_events=LOAD_FILE('my_events.xml') WHERE userid='$userid'";
$result_update = mysql_query($update_xml);

$qtext = "SELECT * FROM load_xml";
$result = mysql_query($qtext);
$numrows = mysql_num_rows ( $result );

echo $numrows.'<br/>';

while ($row = mysql_fetch_row($result)) {

    echo 'user_id '.$row[0].' user_events '.$row[1].'<br/>';

}

mysql_close($con);

?>
  • 写回答

1条回答 默认 最新

  • doubi7739 2012-04-13 19:47
    关注

    Just add the file in the original insert. The file path is that of a file on the server. The mysql user used to connect must have the file privilege. It must be the full path. On windows you need to replace back slashes with forward slashes -

    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    $con = mysql_connect("hostname","adminuser","adminpassword");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("test_db", $con);
    
    $qinsert = "INSERT INTO load_xml (`user_events`) VALUES (LOAD_FILE('/full/path/to/my_events.xml'))";
    $result_insert = mysql_query($qinsert);
    
    $userid = mysql_insert_id();
    echo 'last added id '.$userid.'<br/>';
    
    $qtext = "SELECT * FROM load_xml";
    $result = mysql_query($qtext);
    $numrows = mysql_num_rows ( $result );
    
    while ($row = mysql_fetch_row($result)) {
        echo 'user id '.$row[0].' user_events '.$row[1].'<br/>';
    }
    
    mysql_close($con);
    
    ?>
    

    UPDATE - here is another version using PDO prepared statement and file_get_contents() -

    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    $db = new PDO('mysql:dbname=test_db;host=hostname', 'adminuser', 'adminpassword');
    
    $qinsert = "INSERT INTO load_xml (`user_events`) VALUES (?)";
    $stmt = $db->prepare($qinsert);
    
    $stmt->execute(array(file_get_contents('my_events.xml')));
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题