Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:40:08+00:00 2026-06-02T06:40:08+00:00

I want to load an xml file into a table column with MySQL. How

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:40:10+00:00Added an answer on June 2, 2026 at 6:40 am

    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')));
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need some help i want to load an xml file into a table
I want to load the inside values from this xml code: <?xml version=1.0 encoding=UTF-8?>
I want to load an XML file on the internet into a DataTable in
i am trying to load this xml file into the dropdownlist: http://sites.google.com/site/shadchanproject/Home/lots1.xml i want
I want to load xml file into spring bean for further processing. I know
I want to load key/values configuration pairs stored in XML file. To bind a
We are receiving an XML file from our client. I want to load the
I'm having difficulty doing this seemingly simple task. I want to load XML files
I am currently trying to load a slightly large xml file into a dataset.
i have a XML File i want to insert data from that XML file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.