Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 05-29-2022, 04:50 PM   #1
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,116
Is there a quicker / easier way to do this?

Quote:
<?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'];

?>
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.

Is there an easier way to do this that I'm missing?
__________________
NOTHING TO SEE HERE
Publisher Bucks is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-29-2022, 05:01 PM   #2
Azamat
Confirmed User
 
Azamat's Avatar
 
Industry Role:
Join Date: Dec 2018
Location: Ulaanbaatar, Mongolia
Posts: 61
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'];
__________________
KVS, Mechbunny, API's, Elevatedx, NATS, dating sites, Symfony, Laravel, LAMP stack development.
Azamat is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-29-2022, 05:09 PM   #3
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
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'"
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-29-2022, 05:19 PM   #4
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
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.
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-29-2022, 05:25 PM   #5
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,053
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
Old 05-29-2022, 05:41 PM   #6
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,053
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.

.
__________________
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
Old 05-29-2022, 09:23 PM   #7
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,116
Awesome, thank you all for the responses, will have a play with these suggestions shortly.

Much appreciated
__________________
NOTHING TO SEE HERE
Publisher Bucks is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
easier, reason, database, missing, crap, code, count, top, recipe, display, quicker, page, friendly, recipes, diabetic, based



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.