0

I have tried for importing SQL external file to the database in code igniter please check the given code is correct or not.

If the code is not appropriate then please recommend for newly code///

<!DOCTYPE html>
<html>
<body>

<form action="upload.php">
  Select a file: <input type="file" name="img">
  <input type="submit">
</form>

</body>
</html>

the JavaScript is as follows

<?php

// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;

    // Add this line to the current segment
    $templine .= $line;
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';')
    {
        // Perform the query
        mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
        // Reset temp variable to empty
        $templine = '';
    }
}
echo "Tables imported successfully";
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What is `code ignitor`? :) And what has this question to do with `JavaScript` ? Or `mysqli`, as you are clearly using `mysql`. – Ionut Necula Dec 06 '16 at 10:43
  • i mean to say that i need the fully functional controlllers code for Code Ignitor that helps to import the external sql file for database import. – Gaurav Neupane Dec 07 '16 at 05:02
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Dec 08 '16 at 12:35

0 Answers0