exec_sql_query(DB_NAGIOSQL, $sql)

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
briannd81
Posts: 31
Joined: Sun May 03, 2015 5:41 pm

exec_sql_query(DB_NAGIOSQL, $sql)

Post by briannd81 »

I tried to do a "select * from tbl_host" using the built-in function exec_sql_query available from /usr/local/nagiosxi/html/includes/db.inc.php.

exec_sql_query(DB_NAGIOSQL, $sql)

However, it only returns the "first" row.

The table itself, however, has 1139 rows and I'm expecting all 1139 rows to be returned.

Please advise.
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: exec_sql_query(DB_NAGIOSQL, $sql)

Post by tmcdonald »

What's the exact $sql value? How are you displaying/viewing the results?
Former Nagios employee
briannd81
Posts: 31
Joined: Sun May 03, 2015 5:41 pm

Re: exec_sql_query(DB_NAGIOSQL, $sql)

Post by briannd81 »

It's my mistake. The solution is posted below.

Originally I had this ... the if will return the first row only.

if (($rs = exec_sql_query(DB_NAGIOSQL, $sql))) {
if (!$rs->EOF) {
print strval($rs->fields["br_num"]) . "\n";
$rs->MoveNext();
}
}

Change if to while to go through all elements of the returned object.

if (($rs = exec_sql_query(DB_NAGIOSQL, $sql))) {
while (!$rs->EOF) {
print strval($rs->fields["br_num"]) . "\n";
$rs->MoveNext();
}
}
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: exec_sql_query(DB_NAGIOSQL, $sql)

Post by tmcdonald »

I've made that mistake a few times myself ;)

And just for the record, custom code is usually out of scope for the forum. We can help with it, but at some point we might need to point you to a PHP forum.

I'll be closing this thread now, but feel free to open another if you need anything in the future!
Former Nagios employee
Locked