Jump to content


This topic is now archived and is closed to further replies.


Uploading .sql file to database

Recommended Posts

I need help with determining how to code PHP to allow me to upload my [!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--][u]tables.sql[/u][!--colorc--][/span][!--/colorc--] to my Database. I don't know if the errors lie within my php code, or if the sql file has problems.

Here is the code I have so far:

[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--][!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?php[!--colorc--][/span][!--/colorc--]
[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]$dbhost ='localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to the mysql db');

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--] ?>[!--colorc--][/span][!--/colorc--]
<title>Inserting Tables to MySQL with PHP</title>
[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?php[!--colorc--][/span][!--/colorc--]
[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]mysql_create_db('TESTING-DATABASE'); // creates the database

mysql_select_db('TESTING-DATABASE') or die('Cannot select database');

$queryFile = 'tables.sql';

$fp = fopen($queryFile, 'r');
$query = fread($fp, filesize($queryFile));
$result = mysql_query($query);[!--colorc--][/span][!--/colorc--]
[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]?>[!--colorc--][/span][!--/colorc--]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?php[!--colorc--][/span][!--/colorc--] [!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]mysql_close($conn);[!--colorc--][/span][!--/colorc--] [!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]?>[!--colorc--][/span][!--/colorc--][!--sizec--][/span][!--/sizec--]

So far I can only create the Database, but the file is not uploaded. I know my code may have problems, but I can't find where. Below is what I wanted to upload:

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--][!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]CREATE TABLE `002_news` (
`id_news` bigint(13) NOT NULL auto_increment,
`ueberschrift` varchar(250) default NULL,
`kurztext` longtext,
`autor` varchar(100) default NULL,
`von` date default NULL,
`bis` date default NULL,
`langtext` longtext,
`bild` varchar(30) default NULL,
`weiter` enum('Y','N') NOT NULL default 'N',
PRIMARY KEY (`id_news`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;[!--sizec--][/span][!--/sizec--][!--colorc--][/span][!--/colorc--]

Is it possible that the ENGINE=MyISAM DEFAULT CHARSET=latin1 is the cause? Because when I take it out, the uploading works. But why would this be an issue?


Share this post

Link to post
Share on other sites
I've got it working only if I delete "ENGINE+MYISAM DEFAULT CHARSET=latin1"

However, I can't upload if I have the DROP TABLE IF EXISTS command in the sql:

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--][!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--][u]DROP TABLE IF EXISTS address_book;[/u]
CREATE TABLE address_book (
address_book_id int NOT NULL auto_increment,
customers_id int NOT NULL,
entry_gender char(1) NOT NULL,
entry_company varchar(32),
entry_firstname varchar(32) NOT NULL,
entry_lastname varchar(32) NOT NULL,
entry_street_address varchar(64) NOT NULL,
entry_suburb varchar(32),
entry_postcode varchar(10) NOT NULL,
entry_city varchar(32) NOT NULL,
entry_state varchar(32),
entry_country_id int DEFAULT '0' NOT NULL,
entry_zone_id int DEFAULT '0' NOT NULL,
PRIMARY KEY (address_book_id),
KEY idx_address_book_customers_id (customers_id)

any thoughts?

Share this post

Link to post
Share on other sites
Check your mysql version.
maybe it needs TYPE=MyISAM.
But anyway I think MyISAM is by default.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.