Jump to content

Uploading .sql file to database


netfrugal

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');
mysql_select_db($dbname);[!--colorc--][/span][!--/colorc--]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--] ?>[!--colorc--][/span][!--/colorc--]
<html>
<head>
<title>Inserting Tables to MySQL with PHP</title>
</head>
[!--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));
fclose($fp);
$result = mysql_query($query);[!--colorc--][/span][!--/colorc--]
[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]?>[!--colorc--][/span][!--/colorc--]
<body>


</body>
</html>
[!--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?
Help!

Marc
Link to comment
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)
);[!--sizec--][/span][!--/sizec--][!--colorc--][/span][!--/colorc--]

any thoughts?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.