0

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
);
5
Contributors
4
Replies
9
Views
7 Years
Discussion Span
Last Post by sanchin
0

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

-1

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

?>

My 2ยข. Hope it helps.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.