Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,483 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,747 people online right now. Registration is fast and FREE... Join Now!




MYSQLDB :: User Premmisions and Running .sql files

 
Reply to this topicStart new topic

MYSQLDB :: User Premmisions and Running .sql files

Mike007
27 Feb, 2008 - 06:22 PM
Post #1

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 205


Dream Kudos: 75
My Contributions
I was working on an install script lately and I ran into some problems.

1. I am reading a .sql file that was created by phpmyadmin export tool to create tables and dump information in them. I read it to a variable manipulate it to add table prefixes and try to run the query using mysql_query(). Unfortunatly, it does not work! The script does give me valid SQL code, as i echoed it and tested it, and it worked fine. The reason I believe it doesn't work is maybe because of the newline differences between unix style and windows style. I tried removing that and adding space instead, which i did wrong but it seemed to fix it by adding a space before each newline, but i still don't really understand why that works. I used the regular expression \n\r? instead of \r?\n.
MySQL reports this problem as a syntax error in the sql code...

2. I noticed an annoying bug in my program, since i run my php scripts from a different user account special for the php scripts on my mysql, i did not give it premission to create tables. Simply because i didn't need to at the time. I forgot to change that however, and when i ran the script it didn't create the tables but reported it as a successful installation anyway. Why? I really don't know why mysql_query returns true where it should return false an an error like: Cannot create table, premission denied. So how do I check that the user actually has premission to create a table?
User is offlineProfile CardPM
+Quote Post

Mike007
RE: MYSQLDB :: User Premmisions And Running .sql Files
28 Feb, 2008 - 06:09 PM
Post #2

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 205


Dream Kudos: 75
My Contributions
Ok i seem to have figured out the problem. The problem is with mysql_query, it doesn't really support running multiple queries, using the ';' operator. So i guess i need to come up with a way to create a regular expression to find that deliminator, using explode(";", $str), doesn't really cut it, what if i insert text to the database, it might also contain a semi-colon, not exactly sure how to go about it using regular expressions though. I guess it's time to read a few more chapters smile.gif, conditional expressions maybe. Your help will still be appreciated.
User is offlineProfile CardPM
+Quote Post

Mike007
RE: MYSQLDB :: User Premmisions And Running .sql Files
1 Mar, 2008 - 12:10 AM
Post #3

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 205


Dream Kudos: 75
My Contributions
Ok so i resolved the issue by myself. Here is a function i used to find statements and run them one by one.

php

/**
* Runs SQL statement with multiple commands.
* @param String $sql The sql code.
*/
public function RunMultipleQueries($sql)
{
// split all the query's into an array
$lines = explode("\n", $sql);
$line_num = 0;

while($line_num < sizeof($lines))
{
// remove white spaces
$lines[$line_num] = trim($lines[$line_num]);

// if line is not sql comment
if(strcmp(substr($lines[$line_num], 0, 2), '--') != 0 && $lines[$line_num] != "")
{
$query .= " " . $lines[$line_num];

// check if this is the end of the query, by checking the last character which should be
// a semi-colon after the trim
if(strcmp(substr($query, strlen($query) - 1, 1), ';') == 0)
{
// remove semi-colon
$query = substr($query, 0, strlen($query) - 1);
$this->RunQuery($query);
$query = "";
}
}

// next line, either
$line_num++;
}
}


It finds the end of the sql statement, which has to be terminated by a semi-colon(can stretch over multiple lines). The only down side to this function is if you try using this code:
sql

INSERT INTO table (text) VALUES("This text is ended by a semi-colon and then a new-line character;
making the function break it wrong");


I can of course check the begining of the lines to find if there a begining of a sql statement, using a simple regex. I'll do it later for now it works for me, and will probably post it here as a snippet since i was not really able to find a simple function that does that on the google. Oh yeah and speed test it of course.

Any thoughts guys?

P.S
This has been more like a blog then anything else, lol.. Just drives me crazy when i can't solve a problem, i have to sit down and solve it before anything else.

This post has been edited by Mike007: 1 Mar, 2008 - 12:16 AM
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: MYSQLDB :: User Premmisions And Running .sql Files
1 Mar, 2008 - 07:25 AM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

i made one these , simular to yours.
was posted here for auto db installer, in the forum long time ago.

basicly, it explode ;\n, but your issue with win to nix.
might try rtrim and trim.

faster easeir way might be load data in file.

i usually use comand like mysqldump for big stuff. i usually work with a mysql DB is over 11 gigs atm.
takes it less then an hour to dump it on deticated db server. but i buy this machine i set up striped drives for IO speed.


if you do a str_replace \r
would clean it up and leave the ;\n




This post has been edited by SpaceMan: 1 Mar, 2008 - 07:35 AM
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: MYSQLDB :: User Premmisions And Running .sql Files
1 Mar, 2008 - 08:04 AM
Post #5

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270


if the file gets big will eat the ram.

for big files i would use fgets.

while openfile
if(ereg(--,line)) continue
clean trim remove?
start building a query, concat
if(ereg(;\n,line))
run query



User is offlineProfile CardPM
+Quote Post

Mike007
RE: MYSQLDB :: User Premmisions And Running .sql Files
3 Mar, 2008 - 05:09 PM
Post #6

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 205


Dream Kudos: 75
My Contributions
Thank you SpaceMan for your reply, i defantly see what your saying about the RAM usage. I just didn't know how to read a file line by line, like i would do with other programming languages in php. Now i know you can use fgets() to do that. I'll add a function to read a SQL file like that in addition to the one i already have in my database class utility.

Now about what you said, about the mysqldumb, i'm not exactly sure how to use it. I understand that it is a command line function you can run on your mysql database. If you could show me how to use it using the exec() php function exactly it would be great.

Is the mysqldump alot faster than just reading a file line by line and calling mysql_query multiple times? I mean you call an external program like that, and i'm not sure how exec function goes about doing that.

Finally about the \r they don't seem to give me any problems. Seems like the mysql_query doesn't just ignores all kinds of whitespaces. My problem was supplying more than 1 query in the same function call. And it so happends that the first line was a comment.

P.S
I was unable to find your function/code so if you can give me a link to it, that would really help me out.

This post has been edited by Mike007: 3 Mar, 2008 - 10:52 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:05PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month