View Single Post
Old 05-29-2022, 05:25 PM  
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,055
Quote:
Originally Posted by Azamat View Post
SELECT COUNT(*) AS Total FROM Recipe WHERE Category REGEXP 'diabetic'" something like this, you are not counting in php or in mysql query of your result. Also double check your REGEXP value is correct. https://www.geeksforgeeks.org/mysql-...ssions-regexp/

echo $data['Total'];
Quote:
Originally Posted by Publisher Bucks View Post
I'm trying to display a recipe count at the top of a page based on how many 'diabetic' friendly recipes I have in my database, but for some reason can't get the code above working.

<?php

$con=mysqli_connect "localhost","maindata","pass","user");
$result=mysqli_query("SELECT * FROM Recipe WHERE Category REGEXP 'diabetic'");

$data=mysqli_fetch_assoc($result);
echo $data['total'];

?>

Is there an easier way to do this that I'm missing?
It depends on how you want to use the data.

If further down the page you are going to list the recipes then you would not want to pull the total, you would want to pull the full dataset at the top. To print the total you can echo the number of rows returned by the query.

<?php

$con=mysqli_connect("localhost","maindata","pass", "user");
$result=mysqli_query("SELECT * FROM Recipe WHERE Category REGEXP 'diabetic'");

echo mysqli_num_rows($result);

?>

Then further on you can use the results rather than running another query.

<?php
while($data=mysqli_fetch_assoc($result))
{
Do something here
}
?>

If you are not going to list the recipes later in the page then just pull the total as azamat and redwhiteand blue said above this.

.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote