Anyone know some PHP/SQL?

For system help, all hardware / software topics NOTE: use Coders Corner for all coders topics.

Moderators: Krom, Grendel

Post Reply
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Anyone know some PHP/SQL?

Post 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);
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4641
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Post 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.
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Post 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*
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4641
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Post 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)?
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Post 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.
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4641
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Post 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.
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Post 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.
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Post 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.
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4641
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Post 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).
User avatar
Nitrofox125
DBB Admiral
DBB Admiral
Posts: 1848
Joined: Sun Jul 07, 2002 2:01 am
Location: Colorado Springs, CO, USA
Contact:

Post by Nitrofox125 »

Neato. I just explode(";")'d it and for looped it.
User avatar
Tricord
DBB Alumni
DBB Alumni
Posts: 3394
Joined: Thu Nov 05, 1998 12:01 pm

Post by Tricord »

Right, and when you have a string with a ";" it will bork royally :D
Post Reply