Jump to content

Barand

Moderators
  • Posts

    24,612
  • Joined

  • Last visited

  • Days Won

    834

Everything posted by Barand

  1. Always avoid subqueries like that one - they really slow down the performance of the query. Try SELECT p.first_name , p.last_name , p.id as playerid , sum(runs_scored) as runs , count(runs_scored) as balls , sum(runs_scored = 4 ) as fours , sum(runs_scored = 6) as sixes FROM game g JOIN player p ON g.batting_first = p.team_id JOIN deliveries d ON p.id = d.on_strike_batter_id AND g.id = d.game_id WHERE g.id = 51 GROUP BY p.id; +------------+-----------+----------+------+-------+-------+-------+ | first_name | last_name | playerid | runs | balls | fours | sixes | +------------+-----------+----------+------+-------+-------+-------+ | Brian | Lara | 20 | 58 | 19 | 7 | 2 | | Freddie | Flintoff | 21 | 46 | 14 | 6 | 2 | +------------+-----------+----------+------+-------+-------+-------+
  2. Does this come close to what you were after? SELECT p.first_name , p.last_name , p.id as playerid , sum(runs_scored) as runs FROM game g JOIN player p ON g.batting_first = p.team_id JOIN deliveries d ON p.id = d.on_strike_batter_id AND g.id = d.game_id WHERE g.id = 51 GROUP BY p.id ; +------------+-----------+----------+------+ | first_name | last_name | playerid | runs | +------------+-----------+----------+------+ | Brian | Lara | 20 | 58 | | Freddie | Flintoff | 21 | 46 | +------------+-----------+----------+------+ That is what I thought but what connects a squad to team? EDIT: I would have expected the squad to have a key (team_id, game_id) to show is was the team squad for that game.
  3. Thanks. That puts the players into team 1 or team 2. How does "squad" fit into the picture? What, in your model, is a squad?
  4. Your data model is confusing me regards the (lack of) relationship between player/squad/team. It seems as though there should be one, but I can't see it.
  5. Yes, it can be done, but using a dependent subquery like that is the slowest way to do it. Can you provide a dump of the relevant tables' data and give us some idea of how you want the final output to appear?
  6. Thank you for the pretty pictures. If you had posted a json string that could be copy/pasted intp my editor I might have been able to process it and show you how.
  7. 1 ) Why are you appending this post to a 9 year old topic? 2 ) What has you post got to do with CSS?
  8. Why not store it when it is selected on page 2 then reload/refresh page 1 so it shows the revised list with newly selected item?
  9. How are you storing which products the user has already selected?
  10. No need to mess about exploding the URI - just use $_GET['page']. <style type='text/css'> a { color: gray; text-decoration: none; } a.active { color: red; font-weight: 600; } </style> <?php $selectedPage = $_GET['page'] ?? ''; // get selected page (or empty if there isn't one) $pageArray = array( 'Page1', 'Page2', 'Page3', 'Page4' ); echo "<ul>\n"; foreach ($pageArray as $p) { $act = $selectedPage == $p ? 'active' : ''; // if this is the selected page, add 'active' to class echo "<li><a class='pageitem $act' href='?page=$p'>$p</a></li>"; } echo "</ul>\n"; ?> Result
  11. The easiest way to maintain your list of whitelisted domains is just to list them in a plain text file, say, whitelist.txt domain1.com domain2.com domain3.com ... domain400.com Then use the file() function to load them into your whitelist array. <?php $whitelist = file('whitelist.txt', FILE_IGNORE_NEW_LINES); if (in_array($userDomain, $whitelist)) { // use the domain } else { // reject domain } ?>
  12. Use a database table to store and number your images for you. Mostly we use InnoDB tables but MyISAM tables have a unique property - you can create a two-part primary key where the second part auto-increments. Create table CREATE TABLE `user_image` ( `folder` varchar(20) NOT NULL, `image_no` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(45) DEFAULT NULL, `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`folder`,`image_no`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Add some image records INSERT INTO user_image (folder, username) VALUES ('BIRD', 'Bob'), ('BIRD', 'Bob'), ('BIRD', 'Bob'), ('BIRD', 'Tom'), ('BIRD', 'Tom'), ('BIRD', 'Tom'), ('BIRD', 'Tom'), ('DOG', 'Peter'), ('DOG', 'Peter'), ('DOG', 'Peter'), ('DOG', 'Peter'), ('BIRD', 'Jane'), ('BIRD', 'Jane'), ('DOG', 'Mary'), ('DOG', 'Mary'), ('DOG', 'Mary'), ('DOG', 'Mary'), ('BIRD', 'Jane'), ('BIRD', 'Jane'), ('BIRD', 'Jane'); The data mysql> select * from user_image; +--------+----------+----------+---------------------+ | folder | image_no | username | date_added | +--------+----------+----------+---------------------+ | BIRD | 1 | Bob | 2022-11-01 10:30:58 | | BIRD | 2 | Bob | 2022-11-01 10:30:58 | | BIRD | 3 | Bob | 2022-11-01 10:30:58 | | BIRD | 4 | Tom | 2022-11-01 10:30:58 | | BIRD | 5 | Tom | 2022-11-01 10:30:58 | | BIRD | 6 | Tom | 2022-11-01 10:30:58 | | BIRD | 7 | Tom | 2022-11-01 10:30:58 | | DOG | 1 | Peter | 2022-11-01 10:30:58 | | DOG | 2 | Peter | 2022-11-01 10:30:58 | | DOG | 3 | Peter | 2022-11-01 10:30:58 | | DOG | 4 | Peter | 2022-11-01 10:30:58 | | BIRD | 8 | Jane | 2022-11-01 10:30:58 | | BIRD | 9 | Jane | 2022-11-01 10:30:58 | | DOG | 5 | Mary | 2022-11-01 10:30:58 | | DOG | 6 | Mary | 2022-11-01 10:30:58 | | DOG | 7 | Mary | 2022-11-01 10:30:58 | | DOG | 8 | Mary | 2022-11-01 10:30:58 | | BIRD | 10 | Jane | 2022-11-01 10:30:58 | | BIRD | 11 | Jane | 2022-11-01 10:30:58 | | BIRD | 12 | Jane | 2022-11-01 10:30:58 | +--------+----------+----------+---------------------+
  13. What is the structure of your "senior_dat" table?
  14. Instead of my permanent "flowers" table, create a temporary "flowers" table for the user storing the ist of (100) flowers that they enter then use that in the query I gave earlier. The temporary table will disappear when the script ends and the connection is closed.
  15. It's poor SQL code. Any data returned by the "*" in the select will be meaningless. Because you are using an aggregation function the LIMIT is redundant - there will only be a single row containing the count. It is better to use a column alias for functions $stmt = $pdo->prepare("SELECT count(*) as total FROM users WHERE forgotten_code=?"); $stmt->execute([$_GET['reset']]); $check = $stmt->fetch(); if ($check['total'] > 0) {
  16. Probably because the code isn't correct or because you are looking at the POST data before you have submitted the form, but we aren't looking over your shoulder at your screen so can only guess based on previous experience. The problems you are currently experiencing are basic HTML 101 and nothing to do with PDO
  17. What you don't seem to be aware of is the relation between the names of your form inputs and the index names in the subsequent $_POST array +-------------------+-----------------------+ | Form field name | $_POST element | +-------------------+-----------------------+ | first_name | $_POST['first_name'] | | last_name | $_POST['last_name'] | | email | $_POST['email'] | +-------------------+-----------------------+ which should explain why $_POST['fname'] and $_POST['sname'] are undefined but $_POST['email'] works. Nothing cryptic about it at all, it just requires brain engagement.
  18. There is no way php or mysql can know what isn't in the table unless you tell it what could potentially be there. Create a table "flowers" containing all the varieties you could be storing in "pretty_flowers" table: flowers table: prettyflowers +-----------+ +--------+--------+ | name | | flower | color | +-----------+ +--------+--------+ | carnation | | daisy | yellow | | daffodil | | rose | red | | daisy | | orchid | purple | | fresia | | lily | pink | | lily | +--------+--------+ | orchid | | rose | | tulip | +-----------+ Now you can join the tables using a LEFT JOIN to see which are missing SELECT f.name , p.color FROM flowers f LEFT JOIN prettyflowers p ON f.name = p.flower; Where there is no match, the color will be NULL +-----------+--------+ | name | color | +-----------+--------+ | carnation | | | daffodil | | | daisy | yellow | | fresia | | | lily | pink | | orchid | purple | | rose | red | | tulip | | +-----------+--------+ If you only want to know the missing flowers, add "WHERE p.color IS NULL" to the query
  19. Are you aware that the $_POST data is coming from your form?
  20. $_POST['fname'] and $_POST['sname'] do not exist because there are no input fields in your form with those names.
  21. May have something to do with $_POST['fname'] and $_POST['sname'] not existing anywhere. But it can't tell you that because you stupidly turned off the notifications. (NOTE - doing that does not magically make the errors go away)
  22. Even though it is named "lastmodified", that will record the time the record was inserted. If you want to record when it was modified you need DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT TIMESTAMP
  23. If a pyramid has L layers, you need blocks = L * (L + 1) / 2
  24. I'd criticise you for those too. Yes, you could have used "... FROM joined_chats jc, users u WHERE jc.user_id = u.user_id ..." but I dislike that method for several reasons. it confuses the row selection conditions with join conditions and obfuscates the structure of the query and its table relationships. That syntax can't be used for LEFT/RIGHT OUTER JOINS. Even if you want a cartesian join with no conditions, use tableA CROSS JOIN tableB to be explicit about what is required and you haven't just forgotten the WHERE bits. I have found queries using WHERE JOINS run slower than the same query using explicit JOIN .. ON syntax.
  25. Overwriting your first $stmt object with the second one perhaps? A couple of DON'TS for you... Don't use SELECT * - specify just the columns you need. That makes it more efficient and people like us can see what the query is doing. Don't run queries inside loops like that. Use a single query with a JOIN SELECT jc.user_id , u.name FROM joined_chats jc JOIN users u ON jc.user_id = u.user_id WHERE jc.room_id = ? AND NOW() <= jc.date_expire ORDER BY user_id
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.