Page 1 of 1

Anyone know some PHP/SQL?

Posted: Tue Sep 21, 2004 6:09 am
by Nitrofox125
So I have an SQL query that's generated that I try to run in this PHP file. I have error checking all the way so I know if something goes wrong.

First it connects, no problem with that.
Then it selects the database, no problem.
Then it runs the query... OH NOSE!!! Error!
OK, well fine, so I output the query I'm running right before I run it. If I copy and paste that into a different file and run it, it runs perfectly, no problem. Grrr wtf? I also try outputting dbuser, dbpass, and dbname, and they are all the correct values.

Code: Select all

$dbc=mysql_connect("localhost",$dbuser,$dbpass) or die ('Error: I cannot connect
 to the database because: '.mysql_error());
mysql_select_db($dbname);

$instsql=file_get_contents("install.sql");

//Replace variables in the install file with values you filled in.
$instsql=str_replace("%tableprefix%",$table_prefix,$instsql);
$instsql=str_replace("%adminname%",$admin_user,$instsql);
$instsql=str_replace("%adminpass%",md5($admin_pass1),$instsql);
$instsql=str_replace("%adminemail%",$admin_email,$instsql);

if(mysql_query($instsql)) {
        echo "Installation Successful<BR>";
} else {
        echo "Error while running SQL Query<BR>";
}

mysql_close($dbc);

Posted: Tue Sep 21, 2004 6:27 am
by Sergeant Thorne
Try

Code: Select all

if(mysql_query($instsql)!==false) {
My thinking is this: it's possible (I think) for the mysql_query function to return a value that will evaluate to false in a normal comparison (example: 0), but isn't actually false. Using the Not Identical operator ensures that you only get an error when the function returns a "false".

If this isn't it, try posting a copy of your query string.

Posted: Tue Sep 21, 2004 8:05 am
by Nitrofox125
It's not the error I'm concerned about, it doesn't run the query. When I tell it to make the tables, it gives me that error, and doesn't make the tables. Well, that's all good, but both the SQL code and the query is *immaculate!* But obviously not, because it doesn't work :(

Here's the query I'm trying to run:
http://www.arasian.com/charon/install/install.sql
%stuff% is replaced with real values. If I copy that query and run it in phpMyAdmin, it runs fine. *sigh*

Posted: Tue Sep 21, 2004 10:54 am
by Sergeant Thorne
Do you have the necessary permissions enabled for the MySQL user account you're accessing with PHP (ability to create tables, etc)?

Posted: Tue Sep 21, 2004 10:58 am
by Nitrofox125
Good question *checking*...

Ya, all privileges. It's also not server specific. now I've tried installing it on three servers, and all of them do the same thing.

Posted: Tue Sep 21, 2004 11:57 am
by Sergeant Thorne
PHP Manual - mysql_query() wrote:The query string should not end with a semicolon.
The more I think about it, the more I wonder if this function is even able to handle multiple queries.. try just removing the ';'s first, but you might end up having to create a loop and handle each query individually.

This is of interest to me because I'm learning and working with MySQL through PHP.

P.S. In the future, you would probably get a lot more of a response if you were to post this type of thing in the DBB's Coder's Corner.

Posted: Tue Sep 21, 2004 12:07 pm
by Nitrofox125
Oh, right! I completely forgot there even was a Coder's Corner forum still. *shouts* Can this be moved? Plz!*

I'll try it with a single line query when I have a chance. It should be able to handle multiple queries though, but I don't really know.

Posted: Tue Sep 21, 2004 12:35 pm
by Nitrofox125
OMG MAN YOU'RE SO RIGHT! Wow.... Lol thanks :)

Not completely working yet but this seems to be the way to the solution.

Posted: Tue Sep 21, 2004 12:50 pm
by Sergeant Thorne
Yeah, I just tinkered with it, myself. "mysql_query()" can only handle a single SQL statement. Also, the presence of a semi-colon doesn't have any adverse effects (PHP 4.3.4).

Posted: Tue Sep 21, 2004 3:27 pm
by Nitrofox125
Neato. I just explode(";")'d it and for looped it.

Posted: Wed Sep 22, 2004 7:28 am
by Tricord
Right, and when you have a string with a ";" it will bork royally :D