![]() |
Is there a quicker / easier way to do this?
Quote:
Is there an easier way to do this that I'm missing? :Oh crap |
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']; |
Do you have a column called "total" in your "Recipe" table? If not it won't work, and even if you do it's probably not doing what you think.
You probably want something more like "SELECT COUNT(1) AS total FROM Recipe WHERE Category REGEXP 'diabetic'" |
But, if you are going to be testing this "diabetic" property a lot, I would favour making it a separate column with a boolean type, so it is either true or false. Testing that would be much quicker than doing a string search on each row.
|
Quote:
Quote:
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. . |
Also, if you have fixed categories then you should test for equals instead of a regex match. It will be quicker
....where category='diabetic' And you should have an index on your category column if you do not already. . |
Awesome, thank you all for the responses, will have a play with these suggestions shortly.
Much appreciated :) |
All times are GMT -7. The time now is 05:40 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc