Solved

import mysql database from sql file

Posted on 2006-06-28
8
352 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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