Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Your checkbox name needs to be name= 'member[]' if you want to post an array of multiple values
  2. No, it doesn't according to your form code That's why.
  3. In other words, wasting our time.
  4. Barand

    msqli error

    When selecting from a single table there is no need to qualify all the column names with a table prefix. If you are looking for those existing events that overlap the range $inicio - $fim then you can simplify your query SELECT COUNT(*) AS total FROM `ebspma_paad_ebspma`.`req_material_reserva` WHERE `idequipamento` = $equip AND `idsala` = $sala AND `idtempoInicio` < $fim AND `idTempoFim` > $inicio AND `data` = $data
  5. We have all been there.
  6. Put quotes round the value eg <option value='Channel name'> If you use only <option value=Channel Name> it will use the first word only.
  7. This is the first time you have indicated that $_POST['abc'] is an array and not string. try $updated = array(); $fieldnames = array('id', 'name', 'date'); if (isset($_POST['abc']) && ! empty($_POST['abc'])) { //grab id, name and date and assign to variables foreach ($_POST['abc'] as $abc) { $updated[] = array_combine($fieldnames, explode(',', $abc)); } } echo '<pre>',print_r($updated, true),'</pre>'; Should give something like this Array ( [0] => Array ( [id] => 1 [name] => fred [date] => 2015-05-06 ) [1] => Array ( [id] => 2 [name] => tom [date] => 2015-05-07 ) )
  8. Use + or - the INTERVAL eg Completed in last 7 days DATE(FROM_UNIXTIME(quotes.quote_complete)) > CURDATE() - INTERVAL 7 DAY
  9. Or convert those unix timestamps to DATETIME value AND DATE(FROM_UNIXTIME(quotes.quote_complete)) = CURDATE()
  10. Decided to run the tests again, varying the number of items in the sets (N) from 2 to 10 so I could see how it varied with increases. Method: Re-define N to the number required in each set Run the script to generate the data (code below) Run queries for both methods and take the times (MySql Workbench) Data generation code <?php include ('db_inc.php'); // DB CREDENTIAL DEFINITIONS $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); define("N", 2); // How many values in each set $db->query("DELETE FROM users"); $db->query("DELETE FROM user_hair"); $db->query("DELETE FROM user_body"); $db->query("DELETE FROM user_ethnic"); $values = range(1,15); updateTables($db, $values, 1, 500); // first 500 users updateTables($db, $values, 501, 1000); // second 500 users echo "Finished"; function updateTables($db, $values, $ufrom, $uto) /**** * Function to generate the comma delimited sets * and write records to the link files */ { for ($u=$ufrom; $u<=$uto; $u++) { shuffle($values); $hair = array_slice($values,0,N); // get N random values $body = array_slice($values,1,N); $ethnic = array_slice($values,2,4); $username = "User $u"; $uhvals = join(',', $hair); // create set of N items $ubvals = join(',', $body); $uevals = join(',', $ethnic); $sex = $u%2 + 1; $udata[] = "($u,'$username',$sex,'$uhvals','$ubvals','$uevals')"; foreach ($hair as $h) { // add N records to link tables $hdata[] = "($u,$h)"; } foreach ($ethnic as $e) { $edata[] = "($u,$e)"; } foreach ($body as $b) { $bdata[] = "($u,$b)"; } } $sql = "INSERT INTO users VALUES ".join(',',$udata); $db->query($sql); $sql = "INSERT INTO user_body VALUES ".join(',',$bdata); $db->query($sql); $sql = "INSERT INTO user_hair VALUES ".join(',',$hdata); $db->query($sql); $sql = "INSERT INTO user_ethnic VALUES ".join(',',$edata); $db->query($sql); } ?> The tables (15 types in each of the body_type, hair_color and ethnicity tables so I could select 2 -10 at random) Results attached
  11. I created a table with 1000 users to benchmark the two methods, viz. -- -- METHOD 1 -- SELECT username , GROUP_CONCAT(DISTINCT hc.name) as haircolours , GROUP_CONCAT(DISTINCT e.name) as ethnicities , GROUP_CONCAT(DISTINCT bt.name) as bodytypes FROM users u INNER JOIN body_type bt ON FIND_IN_SET(bt.id,u.body_types) INNER JOIN hair_color hc ON FIND_IN_SET(hc.id,u.hair_colors) INNER JOIN ethnicity e ON FIND_IN_SET(e.id,u.ethnicity) GROUP BY username; -- -- METHOD 2 -- SELECT username , GROUP_CONCAT(DISTINCT hc.name) as haircolours , GROUP_CONCAT(DISTINCT e.name) as ethnicity , GROUP_CONCAT(DISTINCT bt.name) as bodytypes FROM users u INNER JOIN user_body btr ON u.id = btr.user_id INNER JOIN body_type bt ON btr.body_id = bt.id INNER JOIN user_hair hcr ON u.id = hcr.user_id INNER JOIN hair_color hc ON hcr.hair_id = hc.id INNER JOIN user_ethnic et ON u.id = et.user_id INNER JOIN ethnicity e ON et.ethnic_id = e.id GROUP BY username; The first time I ran it, with 3 items in each delimited column, I was surprised that the FIND_IN_SET was faster +------------+---------------+-----------+ | METHOD 1 | FIND_IN_SET | 0.062s | | METHOD 2 | JOINS | 0.125s | +------------+---------------+-----------+ The next time time I increased to 10 items in each list and the FIND_IN_SET degraded significantly +------------+---------------+-----------+ | METHOD 1 | FIND_IN_SET | 3.510s | | METHOD 2 | JOINS | 0.109s | +------------+---------------+-----------+
  12. As I said, if you store in comma delimited strings you cannot use JOINS to the data you need. If you do it correctly as fastsol has shown you then SELECT user_id , GROUP_CONCAT(bt_name SEPARATOR ', ') as bodytypes , GROUP_CONCAT(hc_name SEPARATOR ', ') as haircolours , e.name as ethnicity FROM usertable u INNER JOIN body_type_reference btr ON u.user_id = btr.user_id INNER JOIN body_type bt ON btr.body_type = bt.id INNER JOIN hair_colour_reference hcr ON u.user_id = hcr.user_id INNER JOIN hair_colour hc ON hcr.hair_colour = hc.id INNER JOIN ethnicity e ON u.ethnicity = e.id
  13. Put them in an array $my_numbers = array_keys($rand_nums); Then your variables as $my_numbers[0], $my_numbers[1], ... , $my_numbers[5]
  14. Array keys must be unique, so $rand_nums = []; while (count($rand_nums) < 6) { $rand_nums[mt_rand(1,100)] = 1; } The array keys will be your random numbers
  15. If you store the data as comma delimited strings there is no way you can join those ids to the respective tables. Do you have users with three different ethnicities and four different body types?
  16. Use a table subquery to get the aggregated stuff and match the main table against that on horse and date SELECT uh.HORSE , uh.TRACK , uh.RACE , uh.JOCKEY , uh.TRAINER , uh.OWNER , uh.DATE, , tots.count FROM ULTIMATEHISTORICAL uh INNER JOIN ( SELECT HORSE, , MAX( DATE ) AS DATE , COUNT( * ) AS count FROM ULTIMATEHISTORICAL GROUP BY HORSE ) tots USING (HORSE, DATE) ORDER BY DATE DESC, TRACK ASC, RACE ASC, COUNT DESC
  17. Don't use * in the SELECT, specify the required columns. SELECT horse, , MAX(date) as latest , COUNT(*) as count FROM ULTIMATEHISTORICAL GROUP BY horse ORDER BY latest DESC
  18. @Hyster, Do not open two threads. You are wasting peoples' time answering two posts in parallel.
  19. An alternative approach is SELECT player , GROUP_CONCAT(file) as files FROM players GROUP BY player which wiil give you +----------+--------------------------------------------+ | player | files | +----------+--------------------------------------------+ | hyster | ussr-Object263.png,ussr-Object268.png | | merc | germany-E-100.png,germany-JagdPz_E100.png | +----------+--------------------------------------------+ You can then explode the files column in the results on the comma
  20. If a value is provided, add it to the WHERE conditions. I took your sample input check code from #12 above, that's why there is no sex in there
  21. One thing to watch when using ENUM columns. If the value in the search is numeric it uses the numeric value of the enum. If it is a string it uses the descriptive value. So mysql> SELECT * FROM users WHERE sex = 'female'; +----+----------+--------+ | id | username | sex | +----+----------+--------+ | 2 | User 222 | female | | 4 | User 444 | female | +----+----------+--------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM users WHERE sex = 2; +----+----------+--------+ | id | username | sex | +----+----------+--------+ | 2 | User 222 | female | | 4 | User 444 | female | +----+----------+--------+ 2 rows in set (0.00 sec) But if you put quotes round the '2' it treats it as a string value mysql> SELECT * FROM users WHERE sex = '2'; Empty set (0.00 sec) As for omitting the where criteria that are not required $whereclause = ''; $where = array(); // check "Age Min" dropdown: if (!empty($_POST['age_min'])) { $ageMin = (int) $_POST['age_min']; //echo $ageMin; } else { $ageMin = 18; } // check "Age Max" dropdown: if (!empty($_POST['age_max'])) { $ageMax = (int) $_POST['age_max']; //echo $ageMax; } else { $ageMax = 30; } $where[] = "(TIMESTAMPDIFF(YEAR,dob,CURDATE()) BETWEEN $agemin AND agemax)"; // check "Country" dropdown: if (!empty($_POST['country'])) { $countryId = (int) $_POST['country']; $where = "(u.country = $countryid)" //echo $countryId; } else { $countryId = ''; } if (count($where > 0)) { //any search criteria entered $whereclause = "WHERE " . join(' AND ', $where); } $sql = "SELECT ....." . $whereclause;
  22. What values are you passing to the query?
  23. You check if an input has a value and, if it has, include that condition in the WHERE clause. If it hasn't, leave it out.
  24. This method gets my vote SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age;
  25. @rwhite35, Transactions are not limited to PDO, they are a function of the dbms, not the software used to access it
×
×
  • 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.