I'm writing a small script to retrieve yarn details from database. I use a ajax live search script to retrieve data from the database the basic while loop function works but I want to handle the empty results by submitting a massage to the results div tag.
I did went through the first post which was made about this matter but it didn't solve my problem that's why I ask this from the stacks community. And my problem is not about getting the row count my problem is why the if command is not working.
So in order to that I use the below PHP script.
$searchString = "SELECT yarnId FROM yarndetails WHERE yarnId LIKE concat('%',:searchStr,'%')";
$dbSql = $dbConnect->prepare($searchString);
$dbSql -> bindParam(':searchStr', $_GET["q"]);
$dbSql -> execute();
$getCount = "SELECT COUNT(*) FROM yarndetails WHERE yarnId LIKE concat('%',:sStr,'%')";
$dbCount = $dbConnect->prepare($getCount);
$dbCount -> bindParam(':sStr', $_GET["q"]);
$dbCount -> execute();
$dataCount = $dbCount -> rowCount();
if($dataCount = 1){
while($row = $dbSql -> fetch(PDO::FETCH_ASSOC)) {
echo $row['yarnId'] . "\n";
}
}else{
echo "No Data";
}
I searched StackOverfllow and found out best way to count the rows is to use that "SELECT count(8).." statement but there's something wrong with it some how the if condition passes through directly to the while loop.
Is there a better way to do this or am I missing something if can help please.