Jump to content

[SOLVED] Query syntax (qoutes)


hostfreak

Recommended Posts

Another thing that confuses me is that in php you can only parse a variable in double quotes unless you use the concatenation operator. However, if your doing a query (mysql_query) it will parse the variable surrounded by the single quotes.

 

I am assuming it's only within the mysql_query function that php will parse the variable like this? Do to the fact that inside that function, the quotes will be irrelevant (being as they will then be parsed by mysql)?

Another thing that confuses me is that in php you can only parse a variable in double quotes unless you use the concatenation operator. However, if your doing a query (mysql_query) it will parse the variable surrounded by the single quotes.

 

I am assuming it's only within the mysql_query function that php will parse the variable like this? Do to the fact that inside that function, the quotes will be irrelevant (being as they will then be parsed by mysql)?

 

Keep in mind that the PHP parser is interpreting the entire string before it gets sent to the MySQL query. So, when you have a variable within the single quotes, PHP only knows that it is part of a larger string within the double quotes. If you were to echo the SQL statement out to the screen, you would see what is actually sent to the MySQL parser:

<?php
$name = 'Obsidian';
$sql = "SELECT * FROM users WHERE username = '{$name}'";
echo $sql; // SELECT * FROM users WHERE username = 'Obsidian'
?>

Makes perfect sense now. Thanks for the clarification thus far guys.

 

Only one more question and I think I'll be good. As far as the values needing to be surrounded by quotes in mysql as well, how come it will not give off an error if a value isn't surrounded by quotes?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.