Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. If you have a unique key already set on inventor then inserting a duplicate is already impossible.
  2. Create a unique index on (product, inventor). This will then give a duplicate key error if you attempt to add the same combination Alternatively, having set up that index, change your query to INSERT INTO products (inventar, product, price) VALUES ('$inventar', '$product', '$price') ON DUPLICATE KEY UPDATE price = $price which will update the existing record instead of trying to insert a duplicate
  3. Off-topic but a couple of points about your data. Country_name,city_name and place_name should only appear in their respective tables and not in your log table. That should only contain the place_id. Your place table should contain the city_id. Your city table should contain the country id, so if you know the place then you know the city id; if you then know the city_id you then know the country id and you can join the tables to get the names. Is number always a duplicate value of the id value? country | | +----< city | | +-------< place diver | | | | +--------< log >-------+
  4. DATA: mysql> SELECT * FROM blog; +---------+----------+ | blog_id | blogname | +---------+----------+ | 1 | Blog 1 | | 2 | Blog 2 | | 3 | Blog 3 | | 4 | Blog 4 | | 5 | Blog 5 | +---------+----------+ mysql> SELECT * FROM blogtag; +------------+---------+----------+ | blogtag_id | blog_id | tag | +------------+---------+----------+ | 1 | 1 | any | | 2 | 1 | baker | | 3 | 1 | canary | | 4 | 2 | able | | 5 | 2 | beta | | 6 | 2 | canada | | 7 | 3 | alpha | | 8 | 3 | banana | | 9 | 3 | cup | | 10 | 4 | arachnid | | 11 | 4 | beetle | | 12 | 5 | anchor | +------------+---------+----------+ QUERY (to select one random tag for each blog): SELECT b.blog_id , blogname , SUBSTRING_INDEX(GROUP_CONCAT(tag ORDER BY RAND()),',',1) as tag FROM blog b LEFT JOIN blogtag USING (blog_id) GROUP BY blog_id; RESULT: +---------+----------+--------+ | blog_id | blogname | tag | +---------+----------+--------+ | 1 | Blog 1 | canary | | 2 | Blog 2 | able | | 3 | Blog 3 | banana | | 4 | Blog 4 | beetle | | 5 | Blog 5 | anchor | +---------+----------+--------+
  5. Why the LEFT join? INNER join is much faster And do you need to load the csv file every time you need to run the query?
  6. $lines = array( "The cat jumped over the wheel.", "My four wheels are cool.", "My car has a wheel and a cat on another wheel." ); foreach ($lines as &$line) { $p = strpos($line, 'cat'); if ($p===false) continue; $part1 = substr($line, 0, $p); $part2 = substr($line, $p); $part2 = str_replace('wheel', 'tire', $part2); $line = $part1.$part2; } echo '<pre>',print_r($lines, true),'</pre>'; /* RESULTS ********************************************** Array ( [0] => The cat jumped over the tire. [1] => My four wheels are cool. [2] => My car has a wheel and a cat on another tire. ) */
  7. Corrected version $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = $sires = $dams = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm]; } function getAncestors($id, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id] = $dist; if (isset($dogs[$id]) ) { getAncestors($dogs[$id][0], $dogs, $ancests, $dist+1); getAncestors($dogs[$id][1], $dogs, $ancests, $dist+1); } } $dogid = 1; getAncestors($dogs[$dogid][0], $dogs, $sires, 1); getAncestors($dogs[$dogid][1], $dogs, $dams, 1); ksort($sires); ksort($dams); $common = array_intersect_key($sires,$dams); echo "<pre>"; echo "| ID | NAME | SIRE | DAM |\n"; echo "| | | DIST | DIST |\n"; echo "|-----|--------------------|------|------|\n"; foreach ($common as $id => $dist) { printf("|%4d | %-18s | %4d | %4d |\n", $id, $dogs[$id][2], $sires[$id], $dams[$id]); } | ID | NAME | SIRE | DAM | | | | DIST | DIST | |-----|--------------------|------|------| | 8 | dog I | 3 | 2 | | 16 | dog Q | 4 | 3 | | 17 | dog R | 4 | 3 |
  8. After this line $query = "SELECT * FROM callable WHERE calldate BETWEEN '$calldate' AND '$calldate2' AND clid LIKE '%$clid%' AND channel LIKE '%$channel%' AND duration"; what does this echo $query; give?
  9. Is it feasible for you to load the csv data into a table then join that table to you fewMillionRows table. That way you only need a single SELECT query
  10. Sorry, I copied your code in my last post. The format definition should be in quotes, as in my original post (#4) DATE_FORMAT(invoicedate,'%d-%m-%y'). And don't give them all the same alias (" as date") as you cannot then access them with $row['date']
  11. PS - apologies for the bisexual dog in the data, but you get the idea
  12. Does the array $biglist come from a database table? Are your values indexcol1, indexcol2 etc from the objects in the biglist array?
  13. $query_Invoices = sprintf("SELECT id,username,InvoiceNumber,DATE_FORMAT(invoicedate,%d-%m-%y) as date FROM invoices WHERE username = '%s' ORDER BY invoicedate DESC", $colname_Invoices);
  14. Don't use SELECT *. Specify the fields you need, then it becomes simple to add in the DATE_FORMAT()... as invdate
  15. SELECT DATE_FORMAT(mydatefield, '%d-%m-%Y') as formattedDate If you are sorting or comparing dates, use the original and not the formatted version
  16. Don't add another column for the calculated gains. Derived data should not be stored but calculated when required. For example Data mysql> SELECT * FROM frankchester; +----+-----------+------------+-----------+ | id | accountid | date | followers | +----+-----------+------------+-----------+ | 1 | 1 | 2014-11-10 | 50 | | 2 | 2 | 2014-11-10 | 45 | | 3 | 1 | 2014-11-11 | 55 | | 4 | 2 | 2014-11-11 | 60 | | 5 | 1 | 2014-11-12 | 65 | | 6 | 2 | 2014-11-12 | 55 | +----+-----------+------------+-----------+ Query SELECT accountid, date, followers, gain FROM ( SELECT date , IF(@preva=accountid,followers-@prevf,NULL) as gain , @prevf:= followers as followers , @preva:= accountid as accountid FROM frankchester JOIN (SELECT @prevf:=NULL, @preva:=NULL) as init ORDER BY accountid, date ) as calc; Results +-----------+------------+-----------+------+ | accountid | date | followers | gain | +-----------+------------+-----------+------+ | 1 | 2014-11-10 | 50 | NULL | | 1 | 2014-11-11 | 55 | 5 | | 1 | 2014-11-12 | 65 | 10 | | 2 | 2014-11-10 | 45 | NULL | | 2 | 2014-11-11 | 60 | 15 | | 2 | 2014-11-12 | 55 | -5 | +-----------+------------+-----------+------+
  17. Remove the "%"s
  18. A couple of years back I was working on dog pedigree charts so still had some test data. The chart of the data is attached and, as you can see, there are common ancestors. To find them you need to use a recursive function so I would load your data into an array and use that for the recursive search rather bombard your server with dozens of queries. This code will give the common ancestors and the generational distance on the sire and dam sides. You would then need to calc the IC value for each of these ancestors. $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = $sires = $dams = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm]; } function getAncestors($id, $key, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id] = $dist; if (isset($dogs[$id]) ) { getAncestors($dogs[$id][$key], 0, $dogs, $ancests, $dist+1); getAncestors($dogs[$id][$key], 1, $dogs, $ancests, $dist+1); } } $dogid = 1; getAncestors($dogs[$dogid][0], 0, $dogs, $sires, 0); getAncestors($dogs[$dogid][1], 1, $dogs, $dams, 0); ksort($sires); ksort($dams); $common = array_intersect_key($sires,$dams); echo "<pre>"; echo "| ID | NAME | SIRE | DAM |\n"; echo "| | | DIST | DIST |\n"; echo "|-----|--------------------|------|------|\n"; foreach ($common as $id => $dist) { printf("|%4d | %-18s | %4d | %4d |\n", $id, $dogs[$id][2], $sires[$id], $dams[$id]); } Outputs | ID | NAME | SIRE | DAM | | | | DIST | DIST | |-----|--------------------|------|------| | 8 | dog I | 2 | 1 | | 16 | dog Q | 3 | 2 | | 17 | dog R | 3 | 2 |
  19. Have you tried var_dump($password); to check it contains what you think it contains?
  20. There is no column "res" in your results (you didn't select one with that name or use it as an alias) so $row['res'] has no value. Select the id along with the other columns and use that as the value.
  21. What do you mean by "fails". Not returning any records is not a query failure, it merely means that no records match both conditions. Why are you using LIKE and not = in the password check?
  22. I know the feeling. I can always fall back on "senility" when the need arises.
  23. Test data mysql> SELECT * FROM orders; +----------+------------+--------+ | order_id | order_date | status | +----------+------------+--------+ | 1 | 2014-11-01 | | | 2 | 2014-11-02 | | | 3 | 2014-11-03 | | | 4 | 2014-11-04 | | | 5 | 2014-11-05 | | +----------+------------+--------+ mysql> SELECT * FROM line_items; +--------------+----------+------------+ | line_item_id | order_id | despatched | +--------------+----------+------------+ | 1 | 1 | 0 | | 2 | 1 | 1 | | 3 | 1 | 1 | | 4 | 2 | 1 | | 5 | 2 | 1 | | 6 | 2 | 1 | | 7 | 3 | 0 | | 8 | 3 | 0 | | 9 | 3 | 0 | | 10 | 4 | 0 | | 11 | 5 | 1 | +--------------+----------+------------+ Query to update orders to "COMPLETED" id all items despatched UPDATE orders LEFT JOIN line_items ON orders.order_id = line_items.order_id AND despatched = 0 SET orders.status = 'COMPLETED' WHERE line_items.order_id IS NULL; Resulting orders table mysql> SELECT * FROM orders; +----------+------------+-----------+ | order_id | order_date | status | +----------+------------+-----------+ | 1 | 2014-11-01 | | | 2 | 2014-11-02 | COMPLETED | | 3 | 2014-11-03 | | | 4 | 2014-11-04 | | | 5 | 2014-11-05 | COMPLETED | +----------+------------+-----------+
  24. I shortened your code for you <?php $result = ''; if (isset($_POST['conversionType'])) { $conversionType = $_POST['conversionType']; $conversionInput = $_POST['conversionInput']; $conversionValue = 0; list ($from, $to) = explode('-', $conversionType); $scales = array ( 'C' => 'Celcius', 'F' => 'Fahrenheit', 'K' => 'Kevin', 'R' => 'Rankine' ); define('DEGREES', '°'); switch ($conversionType) { case 'C-F': $conversionValue = $conversionInput * (9/5) + 32; break; case 'C-K': $conversionValue = $conversionInput + 273.15; break; case 'C-R': $conversionValue = ($conversionInput + 273.15) * (9/5); break; case 'K-C': $conversionValue = $conversionInput - 273.15; break; case 'K-F': $conversionValue = $conversionInput * (9/5) - 459.67; break; case 'K-R': $conversionValue = $conversionInput * (9/5); break; case 'F-C': $conversionValue = ($conversionInput - 32) * (5/9); break; case 'F-K': $conversionValue = ($conversionInput + 459.67) * (5/9); break; case 'F-R': $conversionValue = $conversionInput + 459.67; break; case 'R-C': $conversionValue = ($conversionInput + 491.67) * (5/9); break; case 'R-F': $conversionValue = $conversionInput - 459.67; break; case 'R-K': $conversionValue = $conversionInput * (5/9); break; } $result = sprintf('%s%s %s = %0.2f%s %s', $conversionInput, DEGREES, $scales[$from], $conversionValue, DEGREES, $scales[$to]); } ?> <html> <head> <title>Unit Converter</title> </head> <body> <h1>CONVERSION</h1> <form action="" method="post"> <link rel="StyleSheet" media="screen" href="convert.css"> Type of conversion: <br> <select name="conversionType"> <option value="null" selected="selected">Select type...</option> <option value="null"></option> <option value="null">--Temperature--</option> <option value="C-F">Celsius to Fahrenheit</option> <option value="C-K">Celsius to Kelvin</option> <option value="C-R">Celsius to Rankine</option> <option value="K-C">Kelvin to Celsius</option> <option value="K-F">Kelvin to Fahrenheit</option> <option value="K-R">Kelvin to Rankine</option> <option value="F-C">Fahrenheit to Celsius</option> <option value="F-K">Fahrenheit to Kelvin</option> <option value="F-R">Fahrenheit to Rankine</option> <option value="R-C">Rankine to Celsius</option> <option value="R-K">Rankine to Kelvin</option> <option value="R-F">Rankine to Fahrenheit</option> </select> <br><br> Value to convert:<br> <input type="text" name="conversionInput"> <br><br> <input type="submit" value="Convert"> </form> <br><br> <?php echo $result;?> </body> </html>
  25. Check your formulae. C-F should be $conversionValue = $conversionInput * (9/5) + 32; You have (5/9). I haven't checked any other
×
×
  • 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.