0
<?php
$localhost ="localhost";
$username = "root";
$password = "";
$dbname = "sbnhs";
try {
    $db = new PDO("mysql:host=$localhost;dbname:$dbname", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // echo "Success";
    $x = 0128;
    $sql = $db->prepare("SELECT * FROM $dbname.refcitymun WHERE provCode = :provcode");
    $sql->bindValue(':provcode', $x);
    if($sql->execute()) {
        while($result = $sql->fetch()){
            $desc = $result['citymunDesc']; 
            echo $desc;
        }   
    }else {
        echo 'Query Error';
    }
}catch (PDOException $e) {
    print 'Error: ' . $e->getMessage() . '</br>';
    die();
}   

?>

I want to display the data in my address column but it does not display anything nor error. I dont know what is wrong with the code

<?php
$localhost ="localhost";
$username = "root";
$password = "";
$dbname = "sbnhs";
try {
    $db = new PDO("mysql:host=$localhost;dbname:$dbname", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // echo "Success";
    // $x = 0128;
    $sql = $db->prepare("SELECT * FROM $dbname.refcitymun WHERE provCode = 0128");
    // $sql->bindValue(':provcode', $x);
    if($sql->execute()) {
        while($result = $sql->fetch()){
            $desc = $result['citymunDesc']; 
            echo $desc;
        }   
    }else {
        echo 'Query Error';
    }
}catch (PDOException $e) {
    print 'Error: ' . $e->getMessage() . '</br>';
    die();
}   

?>

But when i try the format above it display the data that i want. But i need to use variable in the statement. Please check what is wrong with the first code

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sherwin Obciana
  • 319
  • 1
  • 11

1 Answers1

0

Provide third parameter (appropriate data type) in bindValue:

Change following line:

$sql->bindValue(':provcode', $x);

To

$sql->bindValue(':provcode', $x, PDO::PARAM_STR);// This will work if your provcode is of type int, provide appropriate datatype
Dileep Kumar
  • 1,077
  • 9
  • 14