Jump to content
Sign in to follow this  
niche

Slash using PDO's bindparam

Recommended Posts

here's my simple example:

try {
            $sql = 'SELECT     itemandsize FROM itemandsize WHERE  sub_category  = :sub_category'  ;
            $stmt = $this->db->prepare($sql);
        } catch (PDOException $e) {
            echo $e->getMessage() . '<br>';
        }        
        
        try {
            $stmt->execute(array(':sub_category' => $var));    
        } catch (PDOException $e) {
            echo $e->getMessage() . '<br>';
        }    

$var = 'coffee / tea'

I'm certain that the slash is the problem, but don't know why.  

Do you know why?

I thought treating $var in the array auto resolved all formatting issues.

Obviously not.

$var = 'coffee - tea' works fine

EDIT:

Is it just that slashes are used for escaping?

 

Edited by niche

Share this post


Link to post
Share on other sites

A slash should not cause any problem. Have you checked to see what the error message was when you tried a slash?

Share this post


Link to post
Share on other sites

all error reporting is on. 

no error message is displayed.

EDIT:

Just found out that client manually entered coffee / tea.

Does that make a difference?

 

 

Edited by niche

Share this post


Link to post
Share on other sites

I can't help you solve the problem if I don't know even what the problem is.

Here is what you said:

Quote

$var = 'coffee / tea'

I'm certain that the slash is [the cause of] the problem, but don't know why.  

Do you know why?

You mentioned a potential cause of the problem, but you didn't tell me what the problem actually is. I need to know at least two things about your problem:

  • The behavior you expect from your code
  • What your code is actually doing

A problem only exists if the actual behavior does not match the expected behavior.

 

 

Share this post


Link to post
Share on other sites

Thanks for your patience.

I expect 2 rows returned from the my code.

I get zero rows back and no errors.

I have visually verified that 2 rows exist with the 'coffee / tea' string.

I have also verified that error reporting is working.

Edited by niche

Share this post


Link to post
Share on other sites

If you run that query directly on your database, it returns the results you expect?  I mean, you have at least one row where the sub_category field is set to "coffee / tea"?

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the question jsp.

Things just got weirder.

Yes, I can select 'Coffee / Tea' directly.

Seems I can get the same results even after I  change 'Coffee / Tea' to ' '  in the table (new issue)!

This is a live Godaddy server.

I wonder if it has some kind of a virus or malware? 

everything else works as expected.

EDIT:

Cleared cache, got same results on chrome, ff, and ipad. 

hmmm.

 

 

 

 

Edited by niche

Share this post


Link to post
Share on other sites

If the database has rows containing "coffee / tea" in the sub_category field and the code is exactly as you presented, then the issue has to  be occurring either where the variable was set, or in the code that loops through query results. Start by verifying that $var has the data you expected it to at the moment the query runs.

  • Like 1

Share this post


Link to post
Share on other sites

Turn's out the problem was in my bootstrap.

Thanks to ingolme and jsg.  It was jsg's post that got me to thinking more broadly and ingolme's last post that zeroed in on the real problem.

Thanks again for your patience.

 

 

 

Edited by niche

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×