Jump to content

QueryString question


dstoltz

Recommended Posts

I have this querystring:

$q = sprintf("SELECT * FROM employees WHERE fname LIKE %s OR lname LIKE %s OR badge = %s OR ssn = %s ORDER BY lname",GetSQLValueString($ename,"text"),GetSQLValueString($ename,"text"),GetSQLValueString($ebadge,"int"),GetSQLValueString($essn,"int"));

 

Which results in:

SELECT * FROM employees WHERE fname LIKE 'd' OR lname LIKE 'd' OR badge = 0 OR ssn = 0 ORDER BY lname

 

I want to use a wildcard for the 1st two, like this:

SELECT * FROM employees WHERE fname LIKE 'd%' OR lname LIKE 'd%' OR badge = 0 OR ssn = 0 ORDER BY lname

 

How do I modify the QueryString to do this?

 

I know I can do it in the function call like this:

GetSQLValueString($ename.'%',"text")

 

But I was thinking it would be better to do it by the %s parameter, but this doesn't work:

...WHERE fname LIKE %s\% OR ...

 

Any ideas?

 

Thanks!

Link to comment
Share on other sites

I guess reading the manual is too much work? To escape a type specifier withint sprintf() you use a percent character. Therefore, if you want to literally use the percent character, you would use it twice.

WHERE fname LIKE %s%% OR lname LIKE %s%%

 

But, you can't do that here. Your GetSQLValueString() functions is apparently returning the string with the value already enclosed in parens. So, you can't add the percent sign within the format string. Otherwise you would end up with:

WHERE fname LIKE 'd'% OR lname LIKE 'd'% 

 

You have to add it to the value passed in the GetSQLValueString() function.

 

Link to comment
Share on other sites

Thanks for the reply..

 

BTW, I have been reading the manual, and web sites, and forums, trying to figure this out.

 

Sorry I'm a PHP beginner...

 

I didn't know this forum was only for expert level questions only...

 

Sorry if that seems like a smart-butt response, but your comment of "reading the manual is too much work" ticked me off, considering I tried fixing this for hours.

 

But thanks for your answer.

 

Link to comment
Share on other sites

Sorry I'm a PHP beginner...

 

I didn't know this forum was only for expert level questions only...

 

No, not at all. But, the information you needed was in the manual. It was down in the comments section, but if you are spending hours researching a problem the best place to start is to fully read the manual for the particular function.

With printf() and sprintf() functions, escape character is not backslash '\' but rather '%'.

 

Sorry if I offended you, but I believe in teaching people to fish rather than giving them a fish.

Link to comment
Share on other sites

mjdamato,

 

No sweat - I agree with your fishing analogy. But so you know, I was combing the manual and online sites, but for some reason this solution eluded me. Sometimes even with the answer in front of you, it's not understood. This is why I like forums, because you get a friendly explanation, rather than technical jargon.

 

I'll try and research my questions more thoroughly before posting.

 

Thanks

 

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.