Search code examples
phpmysqlload-data-infile

Loading a help file with PHP code to MySql Database?


have a help file which i have created in Notepad++ with the following syntax

//Filename:PHP_Help.html

<!-- Heading of the Issue/Solution

// it may contain PHP Code or Mysql Queries or General Description
<?php

    // Code that fixed the issue/ Solution to a problem
?>

Some general Description
-->

I used the above format because in Notepad++ if i press "ALT + 0"

it display as

<!-- Heading of issue 1
<!-- Heading of issue 2

so that it helps in finding a solution to a problem reading the heading. But the help file has increased in size and contains hundreds of issues

So i am trying to load the file to database and use PHP to search for the issues/solutions

My Database Schema would be like

Create table issues
(
 id int auto_increment primary key,
 header nvarchar(100),
 content text
);

My Question is how do i load the particular file to database?


Solution

  • // Read the file into a php variable:
    $filecontents = file_get_contents ("PHP_Help.html");
    
    // explode the string into individual records:
    $records = explode ( "<!--" , $filecontents );
    
    // work through the array of records:
    foreach($records as $record) {
        // find your first linefeed
        $headingend = strpos($record, chr(13) );
        // get the heading
        $heading = substr($record, 0, $headingend);
        // and the content
        $content = substr($record, $headingend);
        // write to the database...
    }