I have written some sql codes in a file named sql.sql and I want to install my database using this file. How can I do it? Please help me out....

here is the sql code from the file sql.sql

CREATE DATABASE IF NOT EXISTS wm;
USE wm;

CREATE TABLE IF NOT EXISTS users(
		id        	int primary key auto_increment,
        username  	varchar(25) not null,
        password    varchar,
        email       varchar,
		fullname	varchar
);

You may execute multiple queries with the mysqli interface requires a call to mysqli_multi_query().

here is example

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE wm",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("wm", $con);
$sql = "CREATE TABLE Persons
(
username varchar(25),
password varchar(15),
email varchar
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>

Here's another way, without changing anything in the sql file:

<?php
// Name of the file
$filename = '*****.sql';
// MySQL host
$mysql_host = '*****';
// MySQL username
$mysql_username = '*****';
// MySQL password
$mysql_password = '*****';
// Database name
$mysql_database = '*****';

//////////////////////////////////////////////////////////////////////////////////////////////

// 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_num => $line) {
// Only continue if it's not a comment
if (substr($line, 0, 2) != '--' && $line != '') {
// 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 \'<b>' . $templine . '</b>\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
}

?>

My 2¢. Hope it helps.

Where is the .sql file located? Where should I place it? For example, I am using php in ubuntu. I place my files in /var/www/. Do I place my .sql file in the same folder?
Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.