2012-03-07

spot the mys(ql)take

Spot the mistake time again:

mysql_query(
  "INSERT INTO users (username, password) ↲
    VALUES($user, $pass)", 
  $database
);

Yah. This is a subtle one though, so I don't feel too bad. I almost never use the double-quotes in PHP, so I'm a little rusty with them. mySQL values need to be single-quoted:

mysql_query(
  "INSERT INTO users (username, password) ↲
    VALUES('$user', '$pass')", 
  $database
);

Usually I would avoid having the double-quotes parse variables. Sort of for performance reasons, though I think the difference is probably negligible. Mainly it's a style thing, I guess. Still, I think I'm gonna have to start using it to avoid things like this, which I have been doing up till now:

mysql_query(
  "INSERT INTO users (username, password) ↲
    VALUES('" . $user . "', '" . $pass . "')", 
  $database
);

Makes more sense to me, but it's certainly messier.

No comments:

Post a Comment