Hi,

I'm trying to do the ol' import an sql file into mysql script. My basic codeflow (invisioned) goes something like...

Welcome Page -> Enter DB details (server/un/pw/db name) ->
Verify the .sql file is in the right spot -> Import .sql file -> Upload admin data to db.

I can get everything to post from form to form just fine. I can connect to the db with the posted credentials and even create the database. I just can't import the .sql file successfully.

I can echo out everything that is contained in the file and onto the page just fine. That also happens to be my problem. If I can echo it out (read the file), how do I get it to be imported into MySQL properly?

Here's what I have right now (forum code) to connect and create the db, read the file and then...

// Posted Credentials (from last page)
$host     = $db_server;
$user     = $db_username;
$password = $db_password;
$database = $db_name;

// Connect to db
$conn = MySQL_Connect("$host", "$user", "$password") or die(MySQL_error());

// Create the db from posted data
MySQL_Create_DB("$db_name")or die("<center>Create Error: &nbsp;&nbsp;&nbsp; ".mysql_error());

// Select db
MySQL_Select_DB($database, $conn) or die(mysql_error());

// SQL File to import.  
// I'm doing it this way because, well, I want to = ) $sql_file = substr($_SERVER['PATH_TRANSLATED'], 0,-11).'sql/create_tables.sql'; // Read the sql file and store it into $query $fd = fopen($sql_file, 'rb'); $query = fread($fd, filesize($sql_file)); fclose($fd); // This is what I hoped would work...didn't (what a shock!) MySQL_Query ("$query"); // Close the connection. MySQL_Close ($conn);

Any suggestions on how I can get this to work would be really helpful.

Thanks!

    I tore apart a few scripts and still can't get this thing to go. If anyone even has the simplest of fixes or suggestions or even a link that would be great. Thanks.

    Oh yeah...

    /bump 😃

      Well, to find out what mysql is moaning about you can cal mysql_error(), which will return the last error returned by mysql. It's generally a good idea to display the query at this point as well so that you can see the context of the error. Now for a little code.

      mysql_query ($query) or die($query."<br />\n".mysql_error()); 
      

      HTH
      Bubblenut

        Write a Reply...