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);
?>