Jump to content

Barand

Moderators
  • Posts

    24,429
  • Joined

  • Last visited

  • Days Won

    807

Posts posted by Barand

  1. As I obviously cannot run the script to see for myself, perhaps you would be kind enough to give me an example of the data being passed.

    My main concern is the structure of your code. You are running a query to get an array of ids and then looping through that resulting array to run queries based on those ids.

    Running queries within loops is extremely inefficient. You should be running a single query which joins the tables on those ids to get the data you require.

  2. On 12/20/2019 at 9:37 PM, benanamen said:

    Based on OP's code, he expects there could be a negative int, otherwise he wouldn't be checking for it.

    He's testing for 0 or 1.

    0! and 1! both equate to 1, so no need to calculate any further.

    Every recursive function needs a stop condition otherwise you quickly overflow the stack with an infinite recursion.

    • Like 1
  3. I was assuming that a report_filed meant they attended. But, yes, you could

    mysql> SELECT weekofyear(start_time) as wkno
        ->      , SUM(is_no_show=0 AND is_cancelled=0) as attended
        ->      , SUM(is_no_show=1) as no_shows
        ->      , SUM(is_cancelled=1) as cancellations
        -> FROM heartstring
        -> GROUP BY wkno;
    +------+----------+----------+---------------+
    | wkno | attended | no_shows | cancellations |
    +------+----------+----------+---------------+
    |   40 |        2 |        0 |             0 |
    |   41 |        0 |        1 |             1 |
    |   42 |        1 |        0 |             0 |
    |   43 |        0 |        1 |             0 |
    +------+----------+----------+---------------+

     

    • Thanks 1
  4. It would help to see what your db data  looks like EG, is it

    +------------+-----------+-----------+----------+
    | start_time |  no_shows | cancelled | attended |
    +------------+-----------+-----------+----------+
    | 2019-12-20 |     1     |     0     |     0    |
    | 2019-12-21 |     0     |     0     |     1    |
    | 2019-12-22 |     0     |     1     |     0    |

    If so, then

    SELECT weekofyear(start_time) as weekno
         , SUM(no_shows = 1) as no_shows
         , SUM(cancelled = 1) as cancelled
         , SUM(attended = 1) as attended
    FROM mytable
    WHERE start_time BETWEEN ? AND ?
    GROUP BY weekno;

     

    • Great Answer 1
  5. On 12/22/2019 at 9:15 AM, Fratozzi said:

    $sql_2_2 = "UPDATE analytics SET " . "visitor_visitor_sessions" . "='" . $analytics["visitor_visitor_sessions"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

    $sql_2_3 = "UPDATE analytics SET " . "visitor_visitor_pageviews" . "='" . $analytics["visitor_visitor_pageviews"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

     $sql_2_4 = "UPDATE analytics SET " . "visitor_visitor_pages" . "='" . $analytics["visitor_visitor_pages"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

    becomes...

    $sql_2_2 = "UPDATE analytics 
                SET 
                    visitor_visitor_sessions = ?,
                    visitor_visitor_pageviews = ?,
                    visitor_visitor_pages = ?
                WHERE visitor_visitor_id = ? ";
    $stmt = $this->db->prepare($sql_2_2);
    $stmt->execute( [
                        $analytics['visitor_visitor_sessions'],
                        $analytics['visitor_visitor_pageviews'],
                        $analytics['visitor_visitor_pages'],             
                        $visitor_visitor_id
                    ]);           

     

    • Like 1
  6. The first impression I get when I see a table definition like that is "Spreadsheet!". Relation database tables are not spreadsheets and require data normalization.

    Do not store derived data (Year, Month, Day, Weekday, Hour are all derived from the datetime)

    Do not use "SELECT *". Specify the required columns.

    5 hours ago, Fratozzi said:

    $sql_1_1 = "UPDATE analytics SET " . "visitor_visitor_last_page" . "='" . "" . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";
     $sql_1_2 = "UPDATE analytics SET " . "visitor_session_last_page" . "='" . "" . "' WHERE " . "visitor_session_id" . "='" . $visitor_session_id . "'";

     $sql_1 = $sql_1_1."; ".$sql_1_2.";";
     $this->_db->exec($sql_1);

     That doesn't work, only the $sql_1_1 query will be executed (thankfully as it would open up a whole new vista of SQL injection possibilities).

    5 hours ago, Fratozzi said:

    $sql_2_2 = "UPDATE analytics SET " . "visitor_visitor_sessions" . "='" . $analytics["visitor_visitor_sessions"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

                $sql_2_3 = "UPDATE analytics SET " . "visitor_visitor_pageviews" . "='" . $analytics["visitor_visitor_pageviews"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

                $sql_2_4 = "UPDATE analytics SET " . "visitor_visitor_pages" . "='" . $analytics["visitor_visitor_pages"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";

    You can update more than one column in an update query, you don't need a separate one for each.

    Use prepared queries, do not insert data values directly into query strings.

  7. The code I used gives that format

    mysql> SELECT CASE WHEN FINISHED = 0
        ->         THEN ''
        ->         ELSE DATE_FORMAT(FINISHED, '%d %M %Y')
        ->        END as FINISHED
        -> FROM games
        -> ORDER BY FINISHED DESC
        -> LIMIT 5;
    +------------------+
    | FINISHED         |
    +------------------+
    | 31 October 2016  |
    | 31 December 2016 |
    | 29 October 2018  |
    | 29 June 2019     |
    | 29 January 2017  |
    +------------------+

     

    For future consistency of content you should

    EITHER

    ALTER TABLE `games` 
    CHANGE COLUMN `FINISHED` `FINISHED` DATE NOT NULL DEFAULT '0000-00-00' ;

    to prevent new records having the date set to NULL

    OR

    Change those "0000-00-00" dates back to NULL

×
×
  • 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.