Page 2 of 3

Posted: Wed Apr 11, 2007 1:43 pm
by Jumba
uhm, eh, I'm not that good in PHP so I don't really get it, but you mean that I have to Like make more .sql files?
Like one for dropping the table, one for creating it .. like that?

Posted: Wed Apr 11, 2007 2:04 pm
by feyd
No. It separates them so you can run them individually using a loop and whatever query interface you are using.

Posted: Wed Apr 11, 2007 2:10 pm
by Jumba
feyd wrote:No. It separates them so you can run them individually using a loop and whatever query interface you are using.

uhm, so I put this in the script and it should work then?

Code: Select all

preg_match_all('/^#([^#]+)#$/',$str10,$matches);
print_r($matches);
btw, im sorry if im doing all this wrong, but I'm just learning, I don't get everything ..

Posted: Wed Apr 11, 2007 2:19 pm
by feyd
That's not the code I posted later in the thread.

this is.

Code: Select all

<?php

preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);

?>

Posted: Wed Apr 11, 2007 2:25 pm
by Jumba
feyd wrote:That's not the code I posted later in the thread.

this is.

Code: Select all

<?php

preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);

?>
But on the that thread you put it in one file where the queries were also places in, but where do I have to put it now, in insert.php or the query file? because when I put it in insert.php i get this
Array ( [0] => Array ( ) [1] => Array ( ) ) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; # ---------- CREATE TABLE `Table1` ---------- CREATE TABLE `Table1` ( `ID` ' at line 6
and in the .sql it stays the same = /

Posted: Wed Apr 11, 2007 2:28 pm
by feyd
$text must be the contents of the .sql file.

Posted: Wed Apr 11, 2007 2:38 pm
by Jumba
feyd wrote:$text must be the contents of the .sql file.
I tried that, but I don't really get it, maybe I should just look for another way.
This is to complicated for me, but thanks for the help everyone, I really appreciate it :)

Bye

Posted: Wed Apr 11, 2007 2:52 pm
by feyd
It's extremely simple.

How did you set $text with the contents of the .sql file?

Posted: Wed Apr 11, 2007 3:03 pm
by Jumba
feyd wrote:It's extremely simple.

How did you set $text with the contents of the .sql file?
Well, the code reads .sql files like this

Code: Select all

$sqlfile = "punten.sql";
$handle = fopen($sqlfile, "r");
$sql_query = fread($handle, filesize($sqlfile));
fclose($handle);
Since you said that $text is the contents of the sql file I changed $text with $sql_query since it reads the file there.. right? = /

Posted: Wed Apr 11, 2007 3:22 pm
by feyd
I am unable to produce your errors...

Code: Select all

<?php

$text = file_get_contents('punten.sql');

preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);

?>

Code: Select all

# ---------- MdbToMySQL XP 0.9 MySQL IMPORT FROM `C:\Documents and Settings\Owner\Desktop\Punten.mdb` ----------


# ---------- DROP TABLE `Table1` BEFORE (RE)CREATION ----------

DROP TABLE IF EXISTS `Table1`;

# ---------- CREATE TABLE `Table1` ----------

CREATE TABLE `Table1` (
	`ID` INT NOT NULL AUTO_INCREMENT,
	`Naam` VARCHAR(255) NULL,
	`Toets_Hsft_1` VARCHAR(255) NULL,
	`Toets_Hsft_2` INT NULL,
	`Toets_Hsft_3` INT NULL,
	`HSFT_4` INT NULL,
	PRIMARY KEY (`ID`)
) TYPE=MyISAM;

# ---------- EMPTY TABLE `Table1` ----------

DELETE FROM `Table1`;

# ---------- POPUPLATE TABLE `Table1` ----------

INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (2, 'Calvin', '6', 8, 5, 5);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (3, 'Jenna', '6', 4, 4, 2);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (4, 'Puri', '4', 8, 6, 8);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (5, 'Wakamoli', '8', 9, 8, 6);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (6, 'yakuma', '9', 4, 5, 4);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (7, 'Gizaki', '2', 5, 8, 5);
INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (8, 'Mankala', '3', 6, 10, 2);

# ---------- END OF IMPORT FROM `C:\Documents and Settings\Owner\Desktop\Punten.mdb` ----------
output

Code: Select all

Array
(
    [0] => Array
        (
            [0] => DROP TABLE IF EXISTS `Table1`;

            [1] => CREATE TABLE `Table1` (
        `ID` INT NOT NULL AUTO_INCREMENT,
        `Naam` VARCHAR(255) NULL,
        `Toets_Hsft_1` VARCHAR(255) NULL,
        `Toets_Hsft_2` INT NULL,
        `Toets_Hsft_3` INT NULL,
        `HSFT_4` INT NULL,
        PRIMARY KEY (`ID`)
) TYPE=MyISAM;

            [2] => DELETE FROM `Table1`;

            [3] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (2, 'Calvin', '6', 8, 5, 5);
            [4] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (3, 'Jenna', '6', 4, 4, 2);
            [5] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (4, 'Puri', '4', 8, 6, 8);
            [6] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (5, 'Wakamoli', '8', 9, 8, 6);
            [7] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (6, 'yakuma', '9', 4, 5, 4);
            [8] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (7, 'Gizaki', '2', 5, 8, 5);
            [9] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (8, 'Mankala', '3', 6, 10, 2);

        )

    [1] => Array
        (
            [0] => DROP TABLE IF EXISTS `Table1`
            [1] => CREATE TABLE `Table1` (
        `ID` INT NOT NULL AUTO_INCREMENT,
        `Naam` VARCHAR(255) NULL,
        `Toets_Hsft_1` VARCHAR(255) NULL,
        `Toets_Hsft_2` INT NULL,
        `Toets_Hsft_3` INT NULL,
        `HSFT_4` INT NULL,
        PRIMARY KEY (`ID`)
) TYPE=MyISAM
            [2] => DELETE FROM `Table1`
            [3] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (2, 'Calvin', '6', 8, 5, 5)
            [4] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (3, 'Jenna', '6', 4, 4, 2)
            [5] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (4, 'Puri', '4', 8, 6, 8)
            [6] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (5, 'Wakamoli', '8', 9, 8, 6)
            [7] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (6, 'yakuma', '9', 4, 5, 4)
            [8] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (7, 'Gizaki', '2', 5, 8, 5)
            [9] => INSERT INTO `Table1` (`ID`, `Naam`, `Toets_Hsft_1`, `Toets_Hsft_2`, `Toets_Hsft_3`, `HSFT_4`) VALUES (8, 'Mankala', '3', 6, 10, 2)
        )

)

Posted: Wed Apr 11, 2007 3:41 pm
by Jumba
Hmm, then maybe im Doing soemthing wrong

I'm currently palcing this in insert.php

Code: Select all

<?php

$text = file_get_contents('punten.sql');

preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);

?>
So it comes out as

Code: Select all

<?php

// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.

$mysqlLink = mysql_connect( 'localhost' , 'username' , 'password' );

// Select mySQL Database.

mysql_select_db( 'database' , $mysqlLink );

// Create Query To Create Table.

$sqlfile = "punten.sql";
$handle = fopen($sqlfile, "r");
$sql_query = fread($handle, filesize($sqlfile));
fclose($handle);

$text = file_get_contents('punten.sql');

preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);

// Issue Query.

mysql_query($sql_query, $mysqlLink) or die(mysql_error());

// Close mySQL Connection.

mysql_close( $mysqlLink );



?>
Is this where I am doing it wrong? because I am getting almost the same result as you, but in the end I get
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';

# ---------- CREATE TABLE `Table1` ----------

CREATE TABLE `Table1` (
' at line 6
And the database isn't updated..

Posted: Wed Apr 11, 2007 3:49 pm
by feyd
Yep. You're still calling mysql_query() with $sql_query. You need a loop that iterates over $matches calling mysql_query() with the individual element values.

Posted: Wed Apr 11, 2007 3:52 pm
by Jumba
feyd wrote:Yep. You're still calling mysql_query() with $sql_query. You need a loop that iterates over $matches calling mysql_query() with the individual element values.
But how do I make it look over $matches?
And when that's done, will the script succesfully execute the queries to the database?

Posted: Wed Apr 11, 2007 4:01 pm
by feyd
Do you understand how to make a loop? If you don't, go here: http://php.net/language.control-structures You're interested in the while, do-while, for, and foreach controls.

Provided your queries are valid and do not conflict for some reason, I don't see why they wouldn't work once you get this code functioning correctly.

Posted: Wed Apr 11, 2007 4:12 pm
by Jumba
to be honest I don't get loops at all, so I have no idea how to make it loop, should I make a new thread asking for help with that then? cause I'm really bad at php,