Currently I am failing at the simplest of places. I am counting entries in the database and asking if the value is greater than 2. Even though there are no entries, it gives me my error message that should only be displayed if there are more than 2 entries.
$newdate = date( 'Y-m-d', $fromform->date );
$comparedate = $DB->sql_compare_text('datum');
$comparedateplaceholder = $DB->sql_compare_text(':date');
$compareustd = $DB->sql_compare_text('ustd');
$compareustdplaceholder = $DB->sql_compare_text(':ustd');
$laptops = $DB->get_records_sql(
"SELECT COUNT(*) FROM {bs_steinweg} WHERE
{$comparedate} = {$comparedateplaceholder}
AND
{$compareustd} = {$compareustdplaceholder}",
[
'date' => $newdate,
'ustd' => $fromform->time,
]
);
var_dump($laptops);
if ( $laptops > 2 ) {
$getlaptops = get_string( 'laptopnotfree', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/bs_sw.php', $getlaptops, null, '\core\output\notification::NOTIFY_WARNING' );
} else {
The following gives me: var_dump($laptops);
array(1) { [0]=> object(stdClass)#390 (1) { ["count(*)"]=> string(1) "0" } }
I understand correctly that var_dump() gives me a 0, right?
Where exactly is the error?
Your var_dump
doesn't gives you 0. It shows that the $laptops
variable contain an array of one stdClass
, of which the property count(*)
is equals to 0.
This is because your count(*)
query does returns one row. And it is on this row that you can get your 0.
To get that value, I suggest renaming the column to something easier to use, such as cnt
, and then access the value of this cnt
column in the first row contained in your $laptops
variable.
Something like this:
$laptops = $DB->get_records_sql('select count(*) as cnt ...');
$laptopsCount = $laptops[0]->cnt;
if($laptopsCount > 2 ) {
/* ... */
}
/* ... */