Jump to content

Executing .SQL file via PHP


beaux1

Recommended Posts

I'm not precisely sure which section this is belong to, but I'll put it in MySQL.

 

Basically, I want to be able to run my .SQL file from the PHP script. I know it's possible, as I've seen it been done before, examining other code except I can't exactly work out how it's done. I've googled it, but there's just so many damn ways I've seen of it being done, it confuses the hell out of me.

 

Can anyone share their wisdom with me here?

 

Thans ;)

Link to comment
Share on other sites

you can parse the .sql file , you can use fopen  to read it and use the semicolon as your marker on which the sql ends

make sure to leave the remarks too and the spaces.

 

check out string functions in php, to help you determin where is the start and end of the query statement.

 

good luck.

Link to comment
Share on other sites

Okay, I decided to use file_get_contents :

	$sql = file_get_contents("sql.sql");
// End Retrieve SQL Query 
if (mysql_query($sql)) {

 

Now, my sql.sql file works if I import it into phpMyAdmin, or if I only have one of the two SQL Queries in here.

 

Anyway I can somehow format them so it will read the both of them and work?

 

CREATE TABLE admin (
  ID int(7) NOT NULL auto_increment,
  username varchar(15) default NULL,
  password varchar(32) default NULL,
  PRIMARY KEY  (ID),
  UNIQUE KEY id (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE posts (
  ID int(7) NOT NULL auto_increment,
  name varchar(15) default NULL,
  date varchar(100) default NULL,
  subject varchar(100) default NULL,
  PRIMARY KEY  (ID),
  UNIQUE KEY id (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

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.