2016-11-29 53 views
0

以下是我的PHP代码。我正试图从数据库中检索值,并将它们舍入到最接近的10(仅限上)。此列中数据库中的所有值都是整数。PHP - 尝试对从数据库检索的数据使用吊顶功能时出错

<?PHP 
    @$Teach_ID = $_POST['txtteachID']; 
    @$Class_ID = $_POST['txtclass']; 
    @$BookingDate = $_POST['txtbookingdate']; 
    @$BookingPeriod = $_POST['txtperiod']; 

    require_once('../BookingSystem/DBconnect.php'); 

    $capacity = 'SELECT ClassSize FROM classes WHERE ClassID = 1'; 
     $result = $dbh->query($capacity); 
     $result = (int)$result; 
    function ceiling($number, $significance = 1) 
     { 
      return (is_numeric($number) && is_numeric($significance)) ? (ceil($number/$significance)*$significance) : false; 
     } 
    } 

    if ($result->num_rows > 0) { 
     echo ceiling($result, 10); 
    } 
    ?> 

错误说明

enter image description here

我失去了一些东西明显?

回答

0

你不能这样做$result = (int)$result;。你必须对每行进行刺激,然后提取数据。

+1

这是一个意见,请做一个完整的答案:)向我们展示如何遍历的例子,说明更多的为什么你不能做'(int)$ result;':p – Bobot

+1

对不起。将此编辑转换为完整答案。 –

1

您需要遍历你$result变量,这是一个mysqli_result对象,获取其不同的条目。

使用mysqli_fetch_assoc得到的值,这将是这样的结尾:

$capacity = 'SELECT ClassSize FROM classes WHERE ClassID = 1'; 
$result = $dbh->query($capacity); 
function ceiling($number, $significance = 1) 
{ 
    return (is_numeric($number) && is_numeric($significance)) ? (ceil($number/$significance)*$significance) : false; 
    } 
} 

while ($row = $result->fetch_assoc()) { 
    // if ($result->num_rows > 0) { /* I think this condition is not needed anymore */ 
     $value = intval($row['ClassSize'], 10); // will convert the string from database to an int in base 10 
     echo ceiling($value, 10); /* I suppose you wanted to use the ClassSize key since it's the one you query */ 
    // } 
} 
+0

工作。谢谢。 –

相关问题