How to check whether SELECT EXISTS returns a value or not?

后端 未结 7 1527
后悔当初
后悔当初 2021-01-03 19:38

I am trying to quickly determine if a user_ID is the owner of a \'goal\'. I believe my SQL query is good, but I\'m trying to find a nice way of checking the result!

7条回答
  •  醉酒成梦
    2021-01-03 19:55

    Counting how many rows match the criteria should be easier:

    $sql = SELECT COUNT(*) FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id'
    $query = mysql_query($sql);
    $result = mysql_fetch_row($query);
    
    return $result[0] >= 1;
    

提交回复
热议问题