By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,030 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,030 IT Pros & Developers. It's quick & easy.

how do I query with a string as part of the where clause?

P: 36

I am having a problem with a query. I am trying to query my oracle database for a certain number corresponding to at least 4 other fields of information. Anyway I dont know what is going on but the query doesnt work unless I pull out the part of the clause which contains a string. All other where clauses contain integers stored into php variables but for some reason it doesnt like the string. I would appreciate if someone could take a look at this for me. Heres the code and I threw some comments in to further explain what I am trying to do . Thanks for looking.

Expand|Select|Wrap|Line Numbers
  1. //I get the value of thie variable from a drop down menu which stores it in a 
  2. //session variable.  The contents of the variable is a string.  
  3. $call_put2 = $_SESSION['opt_cp_vals'][$opt_indx];
  6. // Here is the query the problem is when I put $call_put2 it does not work
  7. //Without it, it does work.  I tried escaping the string with single double and no 
  8. // quote but it still does not work
  9. $query7 = "select count(strike) from surfaces where undl_indx = $undl_indx and strike = $user_input_strike and expire_date = $expire_date1 and call_put = \'$call_put2\' ";
Jul 25 '07 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 801
Did you try without single quotes?

[PHP]$query7 = "select count(strike) from surfaces where undl_indx = $undl_indx and strike = $user_input_strike and expire_date = $expire_date1 and call_put = '$call_put2' ";[/PHP]
Escape the quotes in $call_put2.
Jul 25 '07 #2

P: 36
I dont remember if I tried this before. I thought I did and that it just didnt work. But it just worked with the single quotes just now after you told me to try it. I didnt even have to escape in $call_put2. Im so tired and Ive been staring at this script all day. It took me 20 min to get my post coherent enough for another person to understand it. Anyway thanks for taking the time to look through this and respond.
Jul 25 '07 #3

Expert 5K+
P: 5,821
Heya, orfiyus.

Not sure about Oracle, but here's how I'd do it for MySQL:

Expand|Select|Wrap|Line Numbers
  1. $query7 = "
  3.         COUNT(`strike`)
  4.             AS `count`
  5.     FROM
  6.         `surfaces`
  7.     WHERE
  8.     (
  9.             `undl_indx` = '{$undl_indx}'
  10.         AND
  11.             `strike` = '{$user_input_strike}'
  12.         AND
  13.             `expire_date` = '{$expire_date1}'
  14.         AND
  15.             `call_put` = '{$call_put2}'
  16.     )";
Note the use of backticks, and that all values are quoted. Again, Oracle might want you to do things a little differently.

I also like to enclose variables in double-quoted strings in curly-braces, but this is not strictly necessary.
Jul 25 '07 #4

P: 36
Thanks pb but I already got it working. If it aint broke dont fix it. Although the way you typed is easier on the eyes. I ll go back and fix it at the end if I have time.

Jul 25 '07 #5

Expert 5K+
P: 5,821
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Jul 25 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.