[web] Dumb SQL question...

Started by
4 comments, last by Palidine 17 years, 6 months ago
It's been ages since I did this stuff, so pardom my stupidity on this... Can anyone tell me what the problem with this simple little php/mySQL statement is?

mysql_query(
"CREATE TABLE $usersGroupsTableName (
    user_id INT NOT NULL,
    group_id INT NOT NULL);"
) or die(mysql_error());

Produces the following error: 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 '( user_id INT NOT NULL, group_id INT NOT NULL)' at line 1. Maybe I'm missing something (obviously :-) ) but I'm not seeing a syntax problem.
Advertisement
go here http://dev.mysql.com/doc/ and make sure you lookup the syntax for the correct version of MySQL ... it isn't exactly the same between 3.23, 4.0, 4.1, and 5.0
Yeah, I've looked at the docs. Thanks, though.

This is ridiculous! It's such a simple little statement.
Oh, goodness... ok, I got it working. I had initialized $usersGroupTableName but used $usersGroupsTableName ("group" not pluralized in initializer as it should have been)

Dumb mistake. Thanks for taking a look, though.
we've all done it many times ... its the one weakness of working in languages that don't use variable declarations (I love my ruby but I've made been bitten by typos a few times myself).

good luck to you.
Something that helped me infinitely when working with PHP/SQL was to dump the query to a string rather than creating the string inline in the mysql_query statement. Then when you have problems you turn on logging where just before you make each query, you print the string to the page. When the query fails you have the string printed to your browser and you can validate it. You can also then copy/paste it into a terminal window that's connected directly to your SQL database. You'll sometimes get slightly different error messages that way that can help you get to the bug.

-me

This topic is closed to new replies.

Advertisement