Solved

import mysql database from sql file

Posted on 2006-06-28
8
354 Views
Last Modified: 2010-04-30
I have a .sql file, and I want to import data from that file to my mysql database (somthing like phpmyadmin import data function)

How i'm gonna do that?
0
Comment
Question by:huy302
8 Comments
 

Expert Comment

by:vmyth
ID: 17005965
BigDump will help you.
http://www.ozerov.de/bigdump.php
0
 
LVL 6

Expert Comment

by:mattjp88
ID: 17006868
not a PHP app, but pretty cool app I found in my searches: http://www.navicat.com/ .  Not free, but seems to be worth the cost.

-Matt
0
 
LVL 12

Expert Comment

by:str_kani
ID: 17007001
yes you can easily import this using phpmyadmin...
1) select the import option
2) open the sql file in your favourite editor
3) copy and paste the sql commands from your editor into the textarea in the import option
4) or specify the sql in the import option and click go...

5) that's it.
let me know if you need more help using phpmyadmin. :)
Good luck!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17007409
Or ...

<?php
$s_SQL = file_get_contents($s_your_sql_file_name_here);
$r_conn = mysql_pconnect($s_server, $s_username, $s_password) or die('Could not connect : ' . mysql_errno() . ':' . mysql_error());
$r_results = mysql_query($s_SQL) or die('Could not execute query : ' . mysql_errno() . ':' . mysql_error());
mysql_free_results($r_results);
?>

0
 

Author Comment

by:huy302
ID: 17012548
sorry I didn't state myself clearly but, I mean using PHP code to import data, not using programs to do it
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 250 total points
ID: 17016136
Well. The PHP code I supplied would do that.

I use MSSQL. I have an old Sage Retrieve 4GL DB which I need to convert to SQL server for offline reporting.

I have a PHP script which converts the Sage DB to a SQL database, extracts all the data and then pushes the whole lot into SQL server.

All using PHP.

This is a snippet.

// Connect to the SQL Server.
      if (!in_array('VALIDATE', $argv))
            {
            $rConn = mssql_connect('richardquadling','sa','sa') or die("Unable to connect to the master database server.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            $sCreateDB = str_replace("
GO

",";

", file_get_contents('./TripleC.SQL'));
            mssql_query($sCreateDB, $rConn) or die("Unable to create the database.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            mssql_close($rConn);
            $rConn = @mssql_connect('richardquadling','KPI_User','KPI_User') or die("Unable to connect to the database server.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            @mssql_select_db('TripleCDataConversion',$rConn) or die("Unable to connect to the database.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
// Handle additional SQL.
            mssql_query("CREATE VIEW dbo.Distributor_CasingsReceived_CasingsAccepted AS SELECT TOP 100 PERCENT CT.DistributorCode, DI.Name, DI.AddressLines_1, DI.AddressLines_2, DI.AddressLines_3, DI.AddressLines_4, DI.Postcode, CT.TicketPeriod, COUNT(TC.IOCCode) AS CasingsReceived, COUNT(TC.AcceptCode) AS CasingsAccepted FROM dbo.CollectionTickets CT INNER JOIN dbo.TicketCasings TC ON CT.UniqueID = TC.ParentID INNER JOIN dbo.Distributors DI ON CT.DistributorCode = DI.DistributorCode WHERE (TC.IOCCode <> 'N') GROUP BY CT.DistributorCode, DI.Name, DI.AddressLines_1, DI.AddressLines_2, DI.AddressLines_3, DI.AddressLines_4, DI.Postcode, CT.TicketPeriod ORDER BY CT.DistributorCode, CT.TicketPeriod", $rConn) or die("Unable to process additional SQL - CREATE VIEW dbo.Distributor_CasingsReceived_CasingsAccepted.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            mssql_query("CREATE VIEW dbo.TTL_Casings AS SELECT TOP 100 PERCENT CT.TicketNumber, CU.HeadOfficeCode AS CustomerHeadOfficeCode, CT.CustomerCode, DI.Name AS DistributorName, CT.TicketDate, CT.TicketPeriod, TC.CasingSizeCode, TC.MakeCode, TC.SerialNumber, TC.IOCCode, TC.AcceptCode, TC.RejectCode, TC.VehicleNumber, TC.MinTreadDepth, TC.MaxTreadDepth, CASE Regroove WHEN 1 THEN 'Yes' ELSE 'No' END AS Regroove, CM.Description AS CommentDescription, TC.StudLocation, TC.GroupCode FROM dbo.CollectionTickets CT LEFT OUTER JOIN dbo.Customers CU ON CT.CustomerCode = CU.CustomerCode LEFT OUTER JOIN dbo.Distributors DI ON CT.DistributorCode = DI.DistributorCode LEFT OUTER JOIN dbo.TicketCasings TC ON CT.UniqueID = TC.ParentID LEFT OUTER JOIN dbo.CommentCodes CM ON TC.CommentCode = CM.CommentCode WHERE (CT.CustomerCode = '198') ORDER BY CT.TicketDate, CT.TicketNumber, TC.CasingSizeCode", $rConn) or die("Unable to process additional SQL - CREATE VIEW dbo.TTL_Casings.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            }

So, yes, you CAN use PHP code to completely build your DB. The SQL script I have is like the script produced by MSSQL when you ask it to dump the DB ...


/****** Object:  Database TripleCDataConversion    Script Date: 02/02/2006 10:02:30 AM ******/
use [master]
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'TripleCDataConversion')
      DROP DATABASE [TripleCDataConversion]
GO

CREATE DATABASE [TripleCDataConversion]  ON (NAME = N'TripleCDataConversion_Data', FILENAME = N'D:\Data\SQL 2000\MSSQL\Data\TripleCDataConversion_Data.MDF' , SIZE = 1024, FILEGROWTH = 10%) LOG ON (NAME = N'TripleCDataConversion_Log', FILENAME = N'D:\Data\SQL 2000\MSSQL\Data\TripleCDataConversion_Log.LDF' , SIZE = 20, FILEGROWTH = 10%)
 COLLATE Latin1_General_CI_AS
GO

exec sp_dboption N'TripleCDataConversion', N'autoclose', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'bulkcopy', N'false'
GO

exec sp_dboption N'TripleCDataConversion', N'trunc. log', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'torn page detection', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'read only', N'false'
GO

exec sp_dboption N'TripleCDataConversion', N'dbo use', N'false'
GO

etc...

So. All in code.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 7 52
how to access a remote mysql database with xampp 3 24
PHP and MSSQL Arrays and Variables 3 25
Wordpress Query 5 27
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question