Php fetch返回字符串而不是布尔值“true / false”值

时间:2012-04-18 20:40:18

标签: php javascript jquery mysql json

我正在使用完整的日历,我有一些事件是全天活动。通常,我的php设置所有'allDay' => 'false'。现在我注意到如果我没有指定时间,它会增加时间。

我想为所有值设置所有默认值false,除非我将它们指定为true。

我的php fetch如下:

$sql = "SELECT `id`, `title`, `time`, `start`, `end`, `url`, `backgroundColor`, `textColor`, `className`,
   `allDay` FROM calender WHERE length('column') > '0'";

$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row){
    $return[]=array('id'=>$row['id'],
                'title'=>$row['title'],
                "allDay" =>$row['allDay'],
                'start'=>$row['start'].' '.$row['time'],
                'end'=>$row['end'],
                'url'=>$row['url'],
                'backgroundColor'=>$row['backgroundColor'],
                'textColor'=>$row['textColor'],
                'className' =>$row['className']);
}
$dbh = null;

header('Content-type: application/json');
echo json_encode($return);

和jQuery函数是:

$(document).ready(function() {
    $('#calendar').fullCalendar({
    editable: false,
    events: "json-events.php", 
    eventDrop: function(event, delta) {
            alert(event.title + ' was moved ' + delta + ' days\n' +
                '(You cannot update these fields!)');
        },
        loading: function(bool) {
            if (bool) $('#loading').show();
            else $('#loading').hide();
        },
    });
});

我不知道我在哪里添加值。我有mySQL存储'allDay'为true / false但它返回为字符串。所以我真的知道如何在json编码文件之前去掉它。或者在查看数据后让jQuery / javascript更改它。

3 个答案:

答案 0 :(得分:2)

MySQL中的allDay字段是什么类型的?如果它是enum('true', 'false'),那可能是你的问题。这会转换为字符串。我有人这样做,我花了很长时间才弄清楚为什么我认为布尔(或int)是一个字符串。

仔细检查您的数据库并尝试将其设为tinyint(1)并使用0表示false,使用1表示true。 PHP的布尔值true和false将被转换为相应的整数,并且在JavaScript中使用0/1时不会有问题。

答案 1 :(得分:0)

试试这个:

foreach ($result as $row){
    $return[]=array('id'=>$row['id'],
                'title'=>$row['title'],
                "allDay" =>$row['allDay'] == "true",
                'start'=>$row['start'].' '.$row['time'],
                'end'=>$row['end'],
                'url'=>$row['url'],
                'backgroundColor'=>$row['backgroundColor'],
                'textColor'=>$row['textColor'],
                'className' =>$row['className']);
}
$dbh = null;

答案 2 :(得分:0)

你也可以试试这个:

$sql = "SELECT `id`, `title`, `time`, `start`, `end`, `url`, `backgroundColor`, `textColor`, `className`, IF(`allDay` = 'true',true,false) AS allDay  FROM calender WHERE length('column') > '0'";
相关问题