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
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

Link to comment
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
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
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
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
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
Share on other sites

however, php will nonetheless parse them w/out the quotes.

 

After generating a notice, yes.

 

Bah.  Only mortals care about things like so-called "notices." 

 

So I guess you have error turned off then? Well done.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.