如果数据库表为空,则检查

时间:2015-05-20 10:45:48

标签: javascript php sql oop

我有一个从数据库中获取信息的选择器。但是当我的数据库表为空时,选择器仍然显示如下:

what it looks like when my database is empty

但是,当我的数据库为空时。我不想显示选择器。但是有一条消息说:数据库是空的!添点什么。

我的选择器代码:

$results = $database->Selector();
        echo "<form name='form' method='POST' id='selector'>";
        echo "<select name='train_name' id='train_name' multiple='multiple'>";
        // Loop trough the results and make an option of every train_name

        foreach($results as $res){
                    echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
            }  
        echo "</select>";
        echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add to list'/>" . "</td>";
        echo "</form>";

功能:

function selector() {
        $sql = "SELECT train_name, train_id FROM train_information ORDER BY train_name";
        $sth = $this->pdo->prepare($sql);   
        $sth->execute();
        return $sth->fetchAll();
    }

编辑:

现在有了这个:

$results = $database->Selector();
        if(count($results) > 0) {
            //Form etc here//
}else echo "nope";

现在正在运作! :d

0 个答案:

没有答案