-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
If you run the query with the sample data you gave SELECT name , AVG(price) , url_img , avprice FROM test_db JOIN ( SELECT name , AVG(price) as avprice FROM test_db GROUP BY name ) avcalc USING (name) WHERE lot_id = 12346; then you get +----------------+------------+---------+-----------+ | name | AVG(price) | url_img | avprice | +----------------+------------+---------+-----------+ | Aberfledy 20yo | 20.000000 | www. | 24.250000 | +----------------+------------+---------+-----------+ As I told you, AVG(price) is the price of that single lot. The average for those with the same name (given in avprice from the subquery) is 24.25. To output the avprice value after running the query you obviously need to use $row['avprice']
-
If you select the AVG(price) WHERE lot_id = id then that average will always be the price of the single record that is selected. You need to use a subquery to get the av price of the whiskies with the same name. Incidentally, those names should be in a separate table (with an id) and that id should be store in test_db instead of the name. SELECT name , AVG(price) , url_img , avprice FROM test_db JOIN ( SELECT name , AVG(price) as avprice FROM test_db GROUP BY name ) avcalc USING (name) WHERE lot_id = :id";
-
You may find my resource booking tutorial is of some help.
-
It is often better to describe what the problem is rather try to describe your solution to that problem.
-
You could always send me one
-
I altered its color property just to hilight it. You can change the style in the "model" menu EDIT: * how refreshing to see someone correctly use "different from" instead "different than".
-
Sorry, my fault. I was torn between writing pseudocode and actual code. Should be $stmt = $conn->prepare($sql); $stmt->execute( [ 'id' => $_GET['id'] ] ); $row = $stmt->fetch(); echo $row['name'];
-
No. The search stays as it is. That lists all the products matching the search criteria. The user then clicks on one of the links you just added. This takes them to detail.php which queries the db for that selected product and displays the full details (for that one product).
-
The table you needed to add is the "reserva_hora" to give a many-to-many relationship between reserva and hora.
-
Your syntax makes no sense. You need something like if (isset($_GET['id'])) { $sql = "SELECT name, etc FROM test_db WHERE lot_id = :id"; $conn->prepare($sql); $conn->execute( [ 'id' => $_GET['id'] ] ); /// display results }
-
In your link, include the id of the whisky record in the querystring echo "<a href='whisky_details.php?id={$row['id']}' >{$row['name']}</a>;"; In the detail page, query the table for the record with that id (in $_GET['id'] ) then display the details.
-
I added the code tags for you this time. Is it the linking to the whisky details you are having a problem with?
-
I did wonder if the times were predefined, as you have now confirmed. What if the equipment is required for two or more time periods?
-
It really is time that you learnt to use the <> button in the toolbar for your code.
-
My only question is why are the times that the equipment is required stored in a separate "horas" table and not in the "reserva" table? And same question regarding "reserva_detail"?
-
The correct format for MySql dates is YYYY-MM-DD. The format you are trying to save is useless (cannot be compared, cannot be sorted and cannot be used directly with datetime functions) Specify your date column as type DATE. Convert to the correct format either before you insert, or during insert with STR_TO_DATE() function. Example INSERT INTO mytable (mydate) VALUES (str_to_date('20012017', '%d%m%Y') );
-
PHP help mysqli_query ()expects at least 2 parameters, 1 given
Barand replied to akhilkumar332's topic in PHP Coding Help
mysqli_error($con) ^ | +-- you need this -
Syntax error in the query. Remove the comma after "as total". As COUNT(*) now has the column alias "total" you should refer to it as $row['total'] and not $row['COUNT(*)']; It's easier to give expressions like that a column alias, especially for complex expressions.
-
A WHERE condition filters inputs to the query. At that time the totals aren't known. To filter output, use HAVING SELECT username,COUNT(*) as total, FROM games GROUP BY username HAVING total > 5
-
If you aren't searching for the author, don't include the author in the WHERE clause. Similarly, exclude joke_text if there is no value for that. You have a colourful mix of REQUEST, GET and POST variables in your code. Your form uses 'post', so you should be using $_POST. (Actually, as you only want to get data for display, you should be using form method "get"). Don't use REQUEST.
-
The OP's original data only allowed a three-leg solution, so only the final select would have been required. But then it wouldn't find any direct flights or two-leg solutions, if they existed, as they do in my extended data. But at least it does search for solutions. With your query you need to know the result, pass the required flight numbers as parameters to the WHERE clause, then merely output the already known result
-
They were straight out of my imagination, to give me more test options
-
In practice there would be other variables, such as departure day and times. For example, the direct flight may only be available at weekend, Or one leg of a two-leg flight requires an eight hour wait for the second. Then there is price - the three leg flight might be the cheapest option. There is also the question of how you want to use it. If the user only wants direct flights then you might use only the first part of the query then run the second only if there isn't a direct flight available. And the similarly with the three-leg option. So how you script it will depend on circumstance, there isn't one way. The general method would be Define the SQL (done), using placeholders instead of the hard-coded depart and arrive codes. Prepare the query Execute it, passing the depart and arrive as parameters. Process the results as required
-
Of course it does. "%" is the wildcard character which matches anything. It's like searching a directory for *.* - you get all the files.