Best way to check if MySQL results returned in PHP?

前端 未结 11 1079
长情又很酷
长情又很酷 2020-11-28 23:21

I\'m looking for the best way to check and see if any results were returned in a query. I feel like I write this part of code a lot and sometimes I get errors, and sometimes

11条回答
  •  醉话见心
    2020-11-28 23:59

    Whenever we do queries to get some data, it is returned as an object. Then most of us convert it to array for looping through the rows easily. In php "empty()" function is used to check if an array is empty i.e. if it has no data in it. So we can check if returned array representation of query isn't empty by doing like this

    if(!empty($result)){
               //DO STUFF
    }
    

提交回复
热议问题