Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:34:46+00:00 2026-06-12T22:34:46+00:00

I am having a bit of a problem binding a value with PDO and

  • 0

I am having a bit of a problem binding a value with PDO and PHP. When I replace the values with variables the script returns the proper result but for some reason I can’t get this to work.

// Connect to database in function on different page
function db1() {
    try {
        $pdo = new PDO("mysql:host=localhost;dbname=**********", '**********', '**********');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        return $pdo;
        $pdo = null;
    } catch (PDOException $e) {
        die($e->getMessage());
    }
}

// Scrit with problems
$db = db1();
$query = $db->prepare("SELECT column1, column2, column3, column4 FROM table_name WHERE (column4=2 OR column4=3) AND column5=:col1 AND column6=:col2"); // Prepare the statement to prevent any SQL injection
    $query->bindValue(':col1', $var1, PDO::PARAM_INT);
    $query->bindValue(':col2', $var2, PDO::PARAM_STR);
    $query->execute();
// I also tried the following.
// $query->execute(array(':col1' => $var1, ':col2' => $var2));

// Fetch result row
$row = $query->fetch(PDO::FETCH_ASSOC);

Any ideas?


UPDATE
Did var_dump before and after the query. var1 = string(1) “2” and var2 = string(1) “1”

Removed $pdo = null;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:34:48+00:00Added an answer on June 12, 2026 at 10:34 pm

    Except for the $pdo = null line which is after a return and therefore never executed, and the $db = $db1() where the function is used as a variable, the code hasn’t anything obviously wrong. Since you say that replacing the bindings with variables work, I assume the last is a typo.

    So I suggest you try and var_dump what the values of $var1 and $var2 are at that point in the script.

    UPDATE

    I have duplicated the setup like this

    <?php
    
        $db = new PDO("mysql:host=localhost;dbname=test");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        $var1 = 1; // See answer text below
        $var2 = 2;
    
        $query = $db->prepare("SELECT * FROM test WHERE column5=:col1 AND column6=:col2");
        $query->bindValue(':col1', $var1, PDO::PARAM_INT);
        $query->bindValue(':col2', $var2, PDO::PARAM_STR);
        $query->execute();
        $row = $query->fetch(PDO::FETCH_ASSOC);
        print_r($row);
    ?>
    

    Then I created a first table like this

    CREATE TABLE test ( column5 INTEGER, column6 INTEGER );
    

    and then like this

    CREATE TABLE test ( column5 varchar(5), column6 varchar(5) );
    

    and I tried defining $var1 and $var2 as 1,2 (integer) and “1”,”2″ (strings).

    It always worked, i.e., I expected to get a single row as output, and I did:

    Array
    (
       [column5] => 1
       [column6] => 2
    )
    

    At this point I think the only possibility left is something strange in the database content.

    Therefore I also expect the test below, on your system, not to work, i.e. not to return the desired row:

    $query = $db->prepare("SELECT column1, column2, column3, column4 FROM table_name WHERE (column4=2 OR column4=3) AND column5=2 AND column6='1'");
    $query->execute();
    $row = $query->fetch(PDO::FETCH_ASSOC);
    var_dump($row);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit problem with reading argument value in haskell: I'm having options
i'm having a bit of a problem here. I found this script on the
Hello having a bit of problem retrieving a value so i might use it.
I'm having a bit of a problem with converting the result of a MySQL
I am having a bit of a problem here. I have two int values,
I'm having a bit of a problem keeping values from appearing in scope for
Having a bit of a problem, Due to the source-control set-up we use, each
Having a bit of a problem creating an instance of an object. Bear in
I'm having a bit of a problem, drawing text on an image. When I'm
I'm having a bit of a problem using FieldOffset correctly with arrays. The code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.