I have 2 columns in a table. 'phrase' and 'count'.
if($conn->connect_error){ echo 'Connection Faild: '.$conn->connect_error; } else{ $just_str = 'ghsfghffgh'; $sql="select * from dbtest where phrase like '%$just_str%'"; $res=$conn->query($sql); while($row=$res->fetch_assoc()){ $jso = $row["count"]; } } exit(json_encode(array("name"=>$jso)));
I want it to return "Not found" as JSON response if there is no such entry in the database.
Note: $just_str variable is user input.
$just_str
Since you only get one value, you don't need a loop. So just check whether any row is returned.
$row = $res->fetch_assoc(); if ($row) { $jso = $row['count']; } else { $jso = 'Not found'; }
1.4m articles
1.4m replys
5 comments
57.0k users