Click here to Skip to main content
16,005,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
So I am trying to display some items from the database, based on user preferences (aka keywords)

What I'm trying to do is only display e.g 3 recipes at a time. I tried to do an SQL count to count the rows, but doesnt seem to work, only row count i can get is for one item, for example, one item has row count of 3. But i need them for all items.
I tried LIMIT sql as well and didnt work

Is there an easier way to do this than the below? using a while loop which loops through all keywords and displays, however I need something else- i need to display 3 items, and then later on....if something happens, display 1 more??

What I have tried:

    if ($$meal == '1') //whether the user selected the keyword. (1=True)
        {
        $dbQuery = $conn->prepare("select * from Meal where Category='meal' AND Keyword = '" . ($$Keyword) . "'"); //get all recipes where keyword = word user selected
        $dbQuery->execute();
	

    while ($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) 
        {
            echo "//all infromation on this";
		}
				echo $dbQuery->rowCount()."\n";
    } 
}
Posted
Updated 29-Mar-18 6:41am
Comments
Member 13637584 29-Mar-18 13:45pm    
Thanks for these links.

1 solution

If you mean that you want to limit the number of rows to three for example, then you can use LIMIT keyword. Have a look at PHP Limit Data Selections From MySQL[^]

Then again if you mean you want to use 3 keywords at the same time then you can use IN operator, like
SQL
SELECT * 
FROM Meal 
WHERE Category='meal' 
AND Keyword IN ('keyword1','keyword2','keyword3')
 
Share this answer
 
Comments
Member 13637584 29-Mar-18 13:19pm    
Hi I tried the LIMIT 5 with SQL statement, it didnt seem to change it? Is there a way to check this or something on why its not?
Wendelius 29-Mar-18 13:24pm    
Can you post example data and an example for what is the expected result?
Wendelius 29-Mar-18 13:37pm    
Based on your example, do not calculate count. It will give you only one row, the total amount of rows satisfying the conditions.
If you want to fetch first three rows, use something like

SELECT rID, column2, column3, ...
FROM Recipes
WHERE Category = 'Breakfast'
LIMIT 3

If you need to get the rows in some particular order, add an ORDER BY clause

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900