如何获取mysql语句的行数并返回? [重复]

问题描述 投票:0回答:1

我目前在获取代码中的行数时遇到问题。这是我的代码:

$app->get('/banana/:state', function($state) use ($app){
    $db = new DbOperation();
    $today = date("j-M-Y"); 
    if($state == "Indoor"){
    $result = $db->getAllbananaindoor($today);
    $response = array();
    $response['messages'] = array();
    $row = $result->fetch_assoc();

    if($row > 3){
    $temp = array();
    $temp['text'] = 'Yes, you can plant banana seeds today indoors.';
   array_push($response['messages'],$temp);
    }
    else {
    $temp = array();
    $temp['text'] = 'Nope. Today is not a good time to plant banana seeds indoors.';
   array_push($response['messages'],$temp); 
    }
    echoResponse(200,$response);
    }
}




public function getAllbananaindoor($today){
    $stmt = $this->con->prepare("SELECT * FROM garden WHERE humid >? AND time=? AND temp BETWEEN ? AND ?");
    $hum = '50';
    $one = '19';
    $two = '24';
    $stmt->bind_param("iiii",$hum, $today, $one, $two);
    $stmt->execute();
    $students = $stmt->get_result();
    $stmt->close();
    return $students;
}

在此,我从函数 getAllBananaindoor() 获取数据库数据,该函数返回结果,而不是我希望它返回行数,最后检查 $row 是否大于 3,然后进行处理。我怎样才能做到这一点?请帮忙。

php mysql mysqli prepared-statement
1个回答
0
投票

MySQLi Result 对象已定义

num_rows
属性

$students = $stmt->get_result();
return $students->num_rows; // will return the number of rows
© www.soinside.com 2019 - 2024. All rights reserved.