954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

install database in php using .sql file

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
);
avinashmalla
Newbie Poster
3 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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);
?>
enbeeone3
Junior Poster in Training
58 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

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() . '');
// Reset temp variable to empty
$templine = '';
}
}
}

?>


My 2ยข. Hope it helps.

Wraithmanilian
Junior Poster
196 posts since Dec 2009
Reputation Points: 17
Solved Threads: 42
 

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.

sanchin
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: