rahul kapoor
rahul kapoor

Reputation: 51

how to get the number of rows in mysql statements and return it?

I am currently having problem getting the number of rows in my code. here's my code:

$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;
}

In this I get the database data from a function getAllBananaindoor() which returns the result instead i want it return the number of rows and finally check whether $row is greater than 3 and then work on that. how can I do that? please help.

Upvotes: 0

Views: 36

Answers (1)

hjpotter92
hjpotter92

Reputation: 80647

The MySQLi Result object has num_rows property defined.

$students = $stmt->get_result();
return $students->num_rows; // will return the number of rows

Upvotes: 0

Related Questions