Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. You can't do correct comparisons on dates in that format (for example 15th Dec 15 > 14th Dec 16 whereas you need it to be less than). Always store dates in a database as yyyy-mm-dd format (type DATE, DATETIME or TIMESTAMP). To make it usable you have the overhead of using STR_TO_DATE() function to do a compare in a query.
  2. Benchmark rerun with REPLACE also: +----------------------------------------------------------+ | Single query, multiple data | Time | +-----------------------------------------------+----------+ | Insert records | 0.1552 | | Update records | 0.1669 | | Replace records | 0.1449 | +-----------------------------------------------+----------+ +----------------------------------------------------------+ | Single prepare, multiple execute | Time | +-----------------------------------------------+----------+ | Insert records | 66.9068 | | Update records | 69.7397 | | Replace records | 67.3186 | +-----------------------------------------------+----------+
  3. I have just done a benchmark. Each test inserts 1000 records then updates every record. The first pass uses single queries with multiple data values, the updates using the ON DUPLICATE hack. The second pass uses multiple executes of the prepared queries for both update and inserts. The results were +----------------------------------------------------------+ | Single query, multiple data | Time | +-----------------------------------------------+----------+ | Insert records | 0.1222 | | Update records | 0.1441 | +-----------------------------------------------+----------+ +----------------------------------------------------------+ | Single prepare, multiple execute | Time | +-----------------------------------------------+----------+ | Insert records | 62.3395 | | Update records | 68.0629 | +-----------------------------------------------+----------+ The code $N=1000; // set # of records for the benchmark; function resetData($pdo) { $pdo->exec("DROP TABLE IF EXISTS person"); $pdo->exec("CREATE TABLE person ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, fname VARCHAR(40), lname VARCHAR(40) )"); } function showTimings($desc, $t1, $t2, $t3) { echo '<pre>'; echo "+----------------------------------------------------------+\n"; printf("| %-45s | Time |\n", $desc); echo "+-----------------------------------------------+----------+\n"; printf("| %-45s | %8.4f |\n", 'Insert records', $t2-$t1); printf("| %-45s | %8.4f |\n", 'Update records', $t3-$t2); echo "+-----------------------------------------------+----------+\n"; echo '</pre>'; } // // start the tests // resetData($pdo); $t1 = microtime(1); // pdo multiple insert $data=[]; $params=[]; for ($i=1; $i<=$N; $i++) { $fname = 'aaaaa'.$i; $lname = 'bbbbb'.$i; $data[]="(?,?)"; array_push($params,$fname,$lname); } $sql = "INSERT INTO person (fname,lname) VALUES ".join(',', $data); $stmt=$pdo->prepare($sql); $stmt->execute($params); $t2 = microtime(1); // pdo multiple insert..on duplicate updates $data=[]; $params=[]; for ($i=1; $i<=$N; $i++) { $fname = 'ccccc'.$i; $lname = 'ddddd'.$i; $data[]="(?,?,?)"; array_push($params,$i,$fname,$lname); } $sql = "INSERT INTO person (id,fname,lname) VALUES ".join(',', $data) . " ON DUPLICATE KEY UPDATE fname = VALUES(fname), lname = VALUES(lname)"; $stmt=$pdo->prepare($sql); $stmt->execute($params); $t3 = microtime(1); showTimings('Single query, multiple data', $t1, $t2, $t3) ; // // Method 2 - prepare then execute many // resetData($pdo); $t1 = microtime(1); // PDO multiple insert executes $sql = "INSERT INTO person (fname,lname) VALUES (?,?)"; $stmt = $pdo->prepare($sql); for ($i=1; $i<=$N; $i++) { $fname = 'aaaaa'.$i; $lname = 'bbbbb'.$i; $stmt->execute([$fname,$lname]); } $t2 = microtime(1); // PDO multiple update executes $sql = "UPDATE person SET fname = ? , lname = ? WHERE id = ?"; $stmt = $pdo->prepare($sql); for ($i=1; $i<=$N; $i++) { $fname = 'ccccc'.$i; $lname = 'ddddd'.$i; $stmt->execute([$fname,$lname,$i]); } $t3 = microtime(1); showTimings('Single prepare, multiple execute', $t1, $t2, $t3) ;
  4. For a start, too many "UNIONS". SELECT MONTH(due_date) as month , SUM(amount_paid) as total FROM ( SELECT due_date, amount_paid FROM table1 UNION ALL SELECT due_date, amount_paid FROM table2 )x GROUP BY month If you are having to do that, it looks like your table1 and table2 should be a single table with an additional identifier column.
  5. I see you are still adhering to the mantra "Why run one query when six will do?" https://forums.phpfreaks.com/topic/302526-divide-by-zero-warning/?do=findComment&comment=1539284
  6. Yes, that's OK. So long as they are both on the same server. The connection is to the server. And, as you said, you have privileges to access all databases in the query.
  7. Not according to the manual
  8. Have you re-run the code to see what the errors are?
  9. You have several syntax errors. Turn on error reporting and it will tell you where they are. Put this code at top of your code error_reporting(E_ALL); ini_set('display_errors', 1);
  10. Did you run benanamen's code so you can see what your result's structure actually look like? I showed you how to process the results array. What more do you need?
  11. @kian3210 Three things Don't highjack someone else's post That is not PHP code - this is a PHP code help forum (moving to javascript forum) Use code tags for code
  12. I think it's a contest. The first person to work out what his input was wins the prize.
  13. Your $array contains a sub-array (results) of bus journeys, so you need foreach ($array['results'] as $bus) { echo $bus['destination]; // ... etc }
  14. Don't resurrect 6 year old posts
  15. Horses for courses. It depends on where you will use the function. If it is specific to one page, define it in that page. If it is used by more than one page then define it in a separate file. So some will be defined in the page, some in an application-specific function file and some in general function file.
  16. You need to provide your db credentials in the first $pdo line (database, username and password) $dbhost = 'localhost'; $dbuser = '****'; $dbpass = '****'; $database = '****'; $pdo = new PDO("mysql:host=$dbhost; dbname=$database", $dbuser, $dbpass);
  17. Did you create a PDO connection instead of the mysql_connect()? $pdo = new PDO("mysql:host=".HOST.";dbname=".DBNAME,USERNAME,PASSWORD); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  18. Did you change the form method to GET also?
  19. The content data is JSON encoded, so you need to decode it then access the content fields if (isset($_GET['zoeknummer'])) { $sql = "SELECT lead_content FROM lead"; $res = $pdo->query($sql); // where $pdo is your db connection while ($lead = $res->fetchColumn()) { $data = json_decode($lead); if ($data->zoeknummer == $_GET['zoeknummer']) { echo $data->komplex . ' : ' . $data->plaats . '<br>'; } } }
  20. When you pack all your data into a single column you give yourself no option other than select all the data. Construct you table correctly. CREATE TABLE `lead` ( `zoeknummer` int(11) NOT NULL, `komplex` varchar(45) DEFAULT NULL, `plaats` int(11) DEFAULT NULL, `versturen` tinyint(4) DEFAULT NULL, PRIMARY KEY (`zoeknummer`) ) +------------+---------+--------+-----------+ | zoeknummer | komplex | plaats | versturen | +------------+---------+--------+-----------+ | 110022 | test 1 | 1 | NULL | | 112255 | test 2 | 12 | NULL | | 554477 | test 4 | 84 | NULL | | 556478 | test 3 | 51 | NULL | +------------+---------+--------+-----------+ If you are just learning, learn PDO with you MySQL database and don't waste any more time with the mysql_ set of functions. These have been removed from PHP. As you are just fetching data to display, use GET instead of POST. Use POST for updating data. Your code would now be if (isset($_GET['zoeknummer'])) { $sql = "SELECT komplex , plaats FROM lead WHERE zoeknummer = ?"; $stmt = $pdo->prepare($sql); // where $pdo is your db connection $stmt->execute([ $_GET['zoeknummer'] ]); if ($row = $stmt->fetch()) { echo $row['komplex'] . ' : ' . $row['plaats']; } }
  21. try $timestamp = $output2['data'][$acc_id]['last_battle_time']; $dt1 = new DateTime(); $dt1->setTimestamp($timestamp); $dt2 = new DateTime(); echo ceil($dt1->diff($dt2)->days / 7) * 7;
  22. A recursive function is one that calls itself. So you pass an id to the function. The function than finds the next id. It then calls itself passing this new id. I came up with this, but not sure if it does exactly what wanted so you may need to tweak it. <script type="text/javascript"> var divs = []; $().ready(function() { $(".button").click(function() { divs = []; // clear results getLinkedDivs(this.id, "one"); // search data-one getLinkedDivs(this.id, "two"); // search data-two alert(divs.join(", ")); // show results }) }) function getLinkedDivs(id, dnum) { var dnum2 = dnum=="one" ? "two" : "one"; var div1 = $("div[data-"+dnum+"="+id+"]"); if (div1.length) { divs.push($(div1).attr("id")); var id1 = $(div1).data(dnum2); divs.push(id1); getLinkedDivs(id1, dnum); // call itself with next id } } </script>
  23. There are various functions in PHP for displaying dates/times and doing arithmetic with them. Before you use any of them, make sure you have set your default timezone. You can do this in your php.ini file or by using date-default-timezone-set. The easiest to use is the date function.
  24. A better way to do it is to have 1 row for each day for each user. CREATE TABLE `advent` ( `userid` int(11) NOT NULL, `day` int(11) NOT NULL, `opened` tinyint(4) NOT NULL DEFAULT 0 COMMENT '0 or 1', PRIMARY KEY (`userid`,`day`) ) ; Then the update is simply UPDATE advent SET opened = 1 WHERE userid=? and day=?;
×
×
  • 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.