Jump to content

Beggining question


php_guest

Recommended Posts

I am not sure when do I need to use " or ' and when not. A few examples:

1.$result=mysql_query("SELECT * FROM users WHERE user=$user");

2.$result=mysql_query("SELECT * FROM images WHERE user=$row[user]");

3.echo $row[user];

 

Do I need to put in 3rd example $row['user'] or just $row[user] and why? It works also without, is there any security reason? Same questino for first 2 examples.

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/
Share on other sites

Ok, it SHOULD be

 

"SELECT * FROM `users` WHERE `user`='$user'"

 

Table names and column names should be in ` and `, values should be in ' and '

 

it should be $row['user'] just because it's the propper way to call an assosiative index

 

Backticks are not required and in fact should be avoided as they tie your code to mysql.

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/#findComment-929395
Share on other sites

but how can you then write the following?

"SELECT * FROM `users` WHERE `user`='$row['user']'" (this is not correct because ' is already used)

Within strings to echo a array element you use brackets { },

 

$sql = "SELECT * FROM `users` WHERE `user`='{$row['user']}'";

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/#findComment-929423
Share on other sites

but how can you then write the following?

"SELECT * FROM `users` WHERE `user`='$row['user']'" (this is not correct because ' is already used)

Within strings to echo a array element you use brackets { },

 

$sql = "SELECT * FROM `users` WHERE `user`='{$row['user']}'";

 

until it all go to [ ] php6 square brackets will be used only.

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/#findComment-929511
Share on other sites

but how can you then write the following?

"SELECT * FROM `users` WHERE `user`='$row['user']'" (this is not correct because ' is already used)

Within strings to echo a array element you use brackets { },

 

$sql = "SELECT * FROM `users` WHERE `user`='{$row['user']}'";

 

until it all go to [ ] php6 square brackets will be used only.

 

Thats not correct. They are changing the way you access string indexes from {} to [].

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/#findComment-929517
Share on other sites

but how can you then write the following?

"SELECT * FROM `users` WHERE `user`='$row['user']'" (this is not correct because ' is already used)

Within strings to echo a array element you use brackets { },

 

$sql = "SELECT * FROM `users` WHERE `user`='{$row['user']}'";

 

until it all go to [ ] php6 square brackets will be used only.

 

Thats not correct. They are changing the way you access string indexes from {} to [].

Oh, that's still good to know. I tend to use {} to access string indexes just from habit (I guess I like to differentiate between strings and arrays), but I guess it's time change that.

Link to comment
https://forums.phpfreaks.com/topic/176333-beggining-question/#findComment-929710
Share on other sites

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.