Jump to content

[SOLVED] SQL File Script - HUGE Frustration


tsilenzio

Recommended Posts

Okay i been working on this install script for longer then i wanted to, I keep running into endless errors and its starting to really frustrate me i will post the script below and the errors i keep getting :(

 

<?php
/**********************************************************
* This code is the majority of the script, all abstract
* code has been removed, if for whatever reason you
* need to see the rest of the code because I missed
* something then just say so other then that i think
* that i got all the code that you needed to understand
* how it works.
**********************************************************/




//-------- FUNCTIONS --- BEGIN ----------//
function dbConnect()
{
    global $db_hostname, $db_password, $db_username, $db_database;
    
    if ($conn = mysql_connect($db_hostname, $db_username, $db_password))
    {
        if(!mysql_select_db($db_database, $conn))
        {
            die("Couldnt select database:<br><br>" . mysql_error());
        }
        else
        {
            return $conn;
        }
    }
    else
    {
        die("Couldnt connect to MySQL:<br><br>" . mysql_error());
    }
}

function processQuery($query)
{
    global $db_hostname, $db_password, $db_username, $db_database;
  
    $conn = dbConnect();
    
    if($result = mysql_query($query, $conn))
    {
        return $result;
    }
    else
    {
        die("Couldnt send query:<br><br>" . mysql_error());
    }
    mysql_close($result);
}

function injectSQL($fileName)
{   
    $filePath = $_SERVER["DOCUMENT_ROOT"] . '/install/data/' . $fileName;
    
    if(!($handle = fopen($filePath, "r")));
    {
        die("Failed to open file: " . $filePath);
    }
    
    $query = "";
    $lines = explode("\n", fread($handle, filesize($filePath)));
    if(!(fclose($handle)))
    {
        die("Failed to close file: " . $filePath);
    }
    foreach($lines as $line)
    {
        if(!(strpos($line,"//") == 0) && $line != "") //check for commented lines or blanks
        {
            $query .= $line;
            if(!(strpos($line, ";") == false))
            {
                if($fileName = "sql_data.sql")
                {
                    $query = str_replace(array('admin_username', 'admin_password', 'admin_email', 'admin_race', '00000000000000'), array($loginName, $password, $email, $race, time()), $query);
                }
                processQuery($query);
                $query = "";
            }
        }
    }
}

//------ FUNCTIONS --- END ------//




//
// Empty database then drop it
//   ----------------------------------------------------------------------------
//   For those of u wondering i empty the db first because when it recreats it,
//   it will put all the values alreay in it back into it. I dont know why it just does.
//   ----------------------------------------------------------------------------
//
dbConnect();
if($result = mysql_list_tables($db_database))
{
    while ($row = mysql_fetch_row($result))
    {
        print_r($row);
        $table = $row[0];
        $query = "TRUNCATE TABLE $table";
        processQuery($query);
        $query = "DROP TABLE $table";
        processQuery($query);
    }
}
else
{
    die("MySQL Error: " . mysql_error());
}
//
// Create all needed tables
//
injectSQL("sql_tables.sql");
echo("Tables installed."); //Seee if it will even make it this far - Remove once working..

//
// Load values into new tables
//
injectSQL("sql_data.sql");
echo("Data installed."); //Seee if it will even make it this far - Remove once working..

 

 

Result as of now:

Failed to open file: /home/deathsea/domains/dsunlimited.info/public_html/install/data/sql_tables.sql

 

and I cant figure out how to fix it :( any help would be MUCH MUCH MUCH appreciated!!!

Thanks SOOO much in advance!

 

- tsilenzio

Link to comment
https://forums.phpfreaks.com/topic/61755-solved-sql-file-script-huge-frustration/
Share on other sites

Try this:

 

function injectSQL($fileName)

    $filePath = $_SERVER["DOCUMENT_ROOT"] . '/install/data/' . $fileName;

 

echo "$filePath<br>";

 

    if(!($handle = fopen($filePath, "r")));

 

 

 

That should help you determine if your filepath is getting set correctly.  Be sure you can get to the file from that actual path.

 

 

ill try it but i kinda did that it shows up in the error... ill highligh the part for you, if its a diffrent story then let me know:

 

//<?php  -- Just to make it have pretty colors (Syntax Highlight)
function injectSQL($fileName)
{   
    $filePath = $_SERVER["DOCUMENT_ROOT"] . '/install/data/' . $fileName;
    
    if(!($handle = fopen($filePath, "r")));
    {

        die("Failed to open file: " . $filePath);

//<?php  -- Just to make it have pretty colors (Syntax Highlight)
    }
    
    $query = "";
    $lines = explode("\n", fread($handle, filesize($filePath)));
    if(!(fclose($handle)))
    {

        die("Failed to close file: " . $filePath); //Shows what the filepath is

//<?php  -- Just to make it have pretty colors (Syntax Highlight)
    }
    foreach($lines as $line)
    {
        if(!(strpos($line,"//") == 0) && $line != "") //check for commented lines or blanks
        {
            $query .= $line;
            if(!(strpos($line, ";") == false))
            {
                if($fileName = "sql_data.sql")
                {
                    $query = str_replace(array('admin_username', 'admin_password', 'admin_email', 'admin_race', '00000000000000'), array($loginName, $password, $email, $race, time()), $query);
                }
                processQuery($query);
                $query = "";
            }
        }
    }
}

 

and it outputes

 

Failed to open file: /home/deathsea/domains/dsunlimited.info/public_html/install/data/sql_tables.sql

OKAY I finally found the problem it was one character out of place lol when i copied and pasted forgot to get rid of a semicolon ;

 

//<?php // <-- Just to make it have pretty colors (Syntax Highlight) //
if(!($handle = fopen($filePath, "r")));
{
    die("Failed to open file: " . $filePath);
}

 

Thank you everyone who tried to help!!

Archived

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

×
×
  • 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.