0

I'm freaking out here and can't figure out what's wrong. I'm pretty new to PDO, but everything works in my code except for booleans which are sent as strings.

My code (simplified):

$sql = 'SELECT * FROM pages WHERE clean_url_slo = :clean_url_slo AND published = :published LIMIT 1';

$clean_url_slo = 'home';
$published = true;

Then I prepare stuff and execute it like this (simplified):

$stmt = $db->prepare($sql);
$stmt->bindValue(':clean_url_slo',$clean_url_slo,PDO::PARAM_STR);
$stmt->bindValue(':published',$published,PDO::PARAM_BOOL);
$stmt->execute();

And then here is what comes to mysql (from the mysql log - the query mysql received):

91 Query    SELECT * FROM pages WHERE 1=1 AND clean_url_slo='domov' AND published='1' ORDER BY id desc LIMIT 1

As you can see, published is an integer - so always true, which is not OK. Why is that if I'm declaring it as a boolean?

using:

WAMP SERVER PHP version: 5.3.9 Mysql: 5.5.20

Many thanks for your help..

3
  • Integer 1 is TRUE even if quoted as a string, integer 0 is FALSE even if quoted as a string. Commented Apr 1, 2012 at 23:27
  • Test this with SELECT '0'=TRUE; (0), SELECT '0'=FALSE; (1) Commented Apr 1, 2012 at 23:28
  • Thanks for your answers, I have it set to TINYINT(1) in the DB but I thought that BOOL should be 1 or 0 not '1' and '0'. I've tested it now and it really works. Commented Apr 2, 2012 at 6:24

2 Answers 2

2

MySQL does not really have a boolean data type, the BOOLEAN keyword is just an alias of TINYINT(1).
You must work it has an number where 1 = true and 0 = false. Check MySQL Data Types

Sign up to request clarification or add additional context in comments.

Comments

0

For MySql I would use the datatype as TINYINT(1) And set the data type to PARAM_INT,

I have created a small method:

public static function castBoolMysql($value):int
{
    if($value !== 1 && $value !== 0 && !is_bool($value))
    {
        return false;//Incorrect value for bool
    }
    return intval($value);
}

$pdostmt->bindValue(':published', Test::castBoolMysql($published), PDO::PARAM_INT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.