• Advertisement
Sign in to follow this  

[web] [PHP] MySQL Query Being Stuborn

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to do a simple INSERT INTO and PHP is having a fit. I check if the query failed and it did. Now I took the exact query and ran it through MySQL Control Center and it works fine. I checked the 2 variables in it as well, they both have the proper values. What could it be?!
mysql_query("INSERT INTO slots.users (user,password) VALUES ('" . $username . "', '" . $password . "'");
Slots is my DB and users is the table. Both the column names and spelled right and the vars have values. As I said, I ran this through a query in MySQL CC and it worked fine. Help? =/

Share this post


Link to post
Share on other sites
Advertisement
your query has mismatching parentheses

mysql_query(
"INSERT INTO slots.users (user,password) VALUES
(
'" . $username . "', '" . $password . "'"
); // php interpreter rekens this is mysql_query's matching paren.

// missing ');'


should be:

$q = "INSERT INTO slots.users (user,password) VALUES('" .$username. "', '". $password. "');" ;

mysql_query($q);


[Edited by - silvermace on July 31, 2005 1:54:25 PM]

Share this post


Link to post
Share on other sites
omg I'm so retarded, I must have really been tired. Thanks for pointing that out!

Share this post


Link to post
Share on other sites
For clarity, you could have said:

mysql_query("INSERT INTO slots.users SET user='{$username}', password='{$password}'");

(Using 'SET field1=value1, field2=value2, ...' syntax).
If you create a string inside "double quote" markers (rather than inside 'single quote' markers) you can insert variables directly without having to concatenate the strings. For example,

$my_var = "Fish!";
$my_string = "Ben likes the taste of $my_var";


...would set $my_string to Ben likes the taste of Fish! Wrapping the variable you are including in {curly braces} "forces" the variable in instances where it is unclear what is a variable name and what isn't, and makes it easier to read in my opinion. It also lets you include things like values from an array, like:

echo "The page is: {$_SERVER['PHP_SELF']}";

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement