PDO MySQL and php like statement

When I execute this query to the DB:

SELECT * FROM `task` WHERE `date_time_from` like '%0000%'

I get a few results, now I am trying to do the same with PDO and I can not manage to get any results or errors. This is what I have done:

$dbChain = 'mysql:host='.$GLOBALS['dbhost'].';dbname='.$GLOBALS['dbname'];    
try{
        $dbh = new PDO($dbChain, $GLOBALS['dbuser'], $GLOBALS['dbpassword']);
        $sql = "SELECT * FROM task"
                . "WHERE date_time_from like CONCAT('%', :dateFrom, '%')";
        $a = '0000';
        $stmt = $dbh->prepare($sql);
        $stmt->bindParam(':dateFrom', $a);
        $stmt->execute();
        $total = $stmt->rowCount();
        echo $total;
        while ($row = $stmt->fetch()){
            var_dump($row);
        }
    } catch (Exception $e){
        echo 'Error'.$e->getMessage();
    }

The result of this is $total = 0. Can anyone tell me what am I doing wrong?

I have also tried this:

$sql = "SELECT * FROM task"
        . "WHERE date_time_from like :dateFrom";
$a = "%0000%";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':dateFrom', $a);
$stmt->execute();

Same result for $total.


Source: mysql

Leave a Reply