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')));
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题