This topic is locked
[SOLVED]

 Checking result set from DB::Query

10/5/2019 10:51:42 PM
PHPRunner General questions
J
jacktonghk authorDevClub member

I am a newbie to PHPRunner. I use DB::Query to read a table with a where clause. How can quickly check if the result set is empty before doing a while loop to fetch data? Thanks.

Sergey Kornilov admin 10/6/2019

While loop will not be executed if there is no data. There is no need to do anything additionally.
For instance, lets check an example from https://xlinesoft.com/phprunner/docs/db_query.htm

$rs = DB::Query("select * from carsmake");



while( $data = $rs->fetchAssoc() )

{

echo $data["id"];

echo $data["make"];

}


What is inside WHILE loop simply won't run if your query returns no data.

J
jacktonghk authorDevClub member 10/11/2019

Since my result set will return only 1 row or none. I use if ($data = $rs->fetchAssoc()) else .... to show a message if nothing is returned. Thank you.