General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 9th, 2003, 04:25 AM
Alicia Alicia is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 117 Alicia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 29 sec
Reputation Power: 6
using " and '

Hi guys

i would like to ask a question, hope you guys can give me an idea about this as well.. now i have $_COOKIE["cartId"] and want the query to delete according to this cookie value..

so how can i write the query since it already have doube quotes in the cart id... do i need to change the double quote to single ?
e,g: ($_POST["name"],$_COOKIE["var"] and etc)

sample of my code:
mysql_query("delete from cart where cookieId = "$_COOKIE['cartId']"") or die("Query failed: $query<br><br>" . mysql_error());

i tried to use double quote and single as well but parse error occured.. can you please give me an idea how actually i can make this query works in such case... i wish to know when i should use single or double quote.

I read a lot of the books, they mentioned that inappropriate use of " will make parse error... for me, even single quote do have such error..

i did see some they use '".add()."' in their query to refer to a value in function, i am wondering why after added double quote, single quote is still required ???

please advise.

Reply With Quote
  #2  
Old October 9th, 2003, 08:08 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I tend to prefer using the dot operator to concatenate strings:

PHP Code:
 mysql_query("delete from cart where cookieId = '" $_COOKIE["cartId"] . "'") or die("Query failed: $query<br><br>" mysql_error()); 


In this example, I happen to be printing the first part of the query, with an opening single quote around the cartId value, then sticking in the value of the cartId, and then the closing quote. You're already doing this in your die() code. There are other ways of doing this, but my personal style is to always split variables out of strings and concatenate them.

Reply With Quote
  #3  
Old October 9th, 2003, 09:57 AM
mattp23 mattp23 is offline
Moderated
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 82 mattp23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 44 sec
Reputation Power: 6
There is quite a big difference between ' and ".
Any string between a pari of single-qoutes ( ' ) is treated as a literal string, that is php does no work with it, whare as a string between a pair of doulbe-quotes ( " ) variables are parsed within it.
To explain:
PHP Code:
 $name 'matt';

  echo 
'hello';
  
// outputs: hello

  
echo 'hello $name';
  
// outputs: hello $name

  
echo 'hello ' $name;
  
// outputs: hello matt

  
echo "hello $name";
  
// outputs: hello matt 

Now many people code using the fourth example, it just seems easier, However php will take a speed hit. In small programs this may not seem like much, but if this was within a loop, or run on a slower computer, it may get quite significant, especially when you go on to write larger scripts!

Remember when you want to have special characters within strings they must be escaped (by placing a backslash infront of them):
\n linefeed ( newline )
\r carriage return
\t horizontal tab
\\ backslash
\$ dollar sign
\" double-quote
\' single-quote
( You should note that the single quote above does not have a slash before it, this is an apparent bug in the forum, it should have a slash before it! In my example code below remember this, and insert shlashes as appropriate otherwise you will get errors when you try to run the script. )

My message is always try to use single-quotes ( ' ) when you can, and conc strings ( 'this and ' . $this ) This is probably a better way to code in most circumstances and a habit you should get used to.

Therefore your code would be something like this:
PHP Code:
 $query 'DELETE FROM cart WHERE cookieId = \'' $_COOKIE['cartId'] . '\'';
mysql_query$query )
  or die( 
'Query failed: ' $query '<br><br>' mysql_error() ); 


Hope this Helps

Reply With Quote
  #4  
Old October 9th, 2003, 02:50 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
I like to use the { and }... so:
PHP Code:
 mysql_query("delete from cart where cookieId = '${_COOKIE['cartId']}'") or die("Query failed: $query<br><br>" mysql_error()); 

And yes, you should be using single quotes around your array indexes unless they are numeric.
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #5  
Old October 9th, 2003, 02:52 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Quote:
And yes, you should be using single quotes around your array indexes unless they are numeric. [/B]


Wonder if you could explain why. Using the double quotes is an old habit that'll die really hard for me (muscle memory). Is there a signifcant difference in performance when using single quotes? Or is this just a convention? Sorry to hijack.

Reply With Quote
  #6  
Old October 9th, 2003, 02:53 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
In addition, it is better to put your query strings in a variable and pass that variable to mysql_query:
PHP Code:
 $sql "delete from cart where cookieId = '${_COOKIE['cartId']}'";
mysql_query($sql) or die... 

because now you can do some quick (but nasty) debugging
PHP Code:
 $sql "delete from cart where cookieId = '${_COOKIE['cartId']}'";
exit(
$sql); // shows you what is in your query... you can paste this into mysql and actually see if it works.
mysql_query($sql) or die... 

Reply With Quote
  #7  
Old October 9th, 2003, 03:44 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
One good reason is that you can do this:
PHP Code:
 $sql "delete from cart where cookieId = '${_COOKIE['cartId']}'"
but can't do this:
PHP Code:
 $sql "delete from cart where cookieId = '${_COOKIE["cartId"]}'"
Above and beyond instances like that, you will also notice that it is just clearer that you are referencing a literal string such as 'string' where '$string' would be invalid, however, "$string" is valid. Sort of self documenting. Yes, I believe that in most languages the literal string is faster because it doesn't have to worry that something in there might be a varialbe (because it isn't allowed), so it needs no parsing.

Reply With Quote
  #8  
Old October 9th, 2003, 04:19 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
That makes sense. Thanks for the explanation.

Reply With Quote
  #9  
Old October 10th, 2003, 12:35 AM
Alicia Alicia is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 117 Alicia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 29 sec
Reputation Power: 6
if i put $ sign outside of the brace like you guys do, wun it cause any error ??? if that is a legal way to do it, may i know what is the difference if i put it inside the " like " .$_COOKIE['name']. " and ${_COOKIE["cartId"]}..

it looks similar to me... please advise.

Reply With Quote
  #10  
Old October 10th, 2003, 01:23 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
First issue is that this:
PHP Code:
"${_COOKIE["cartId"]}" 
won't work.
This:
PHP Code:
"${_COOKIE['cartId']}" 
will work.

The difference in this method as opposed to the concatenation is just that... concatenation is a little harder to read (some may argue against that)... but basically, why concatenate when you don't have to? It's kind of like scotch taping your cloths into your closet when you can simple use a hanger. If scotch tape is all you have thats fine, but if you have some hangers available, why not use them?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > using " and '


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT