Jump to content

bubblegum.anarchy

Members
  • Posts

    526
  • Joined

  • Last visited

    Never

Posts posted by bubblegum.anarchy

  1. Any fields that will potentially be left blank should be declared as NULL i.e.:

     

    CREATE TABLE `people` (
      `id` int(3) NOT NULL auto_increment,
      `name` var(50) NOT NULL.
      `type` varchar(100) NOT NULL,
      `location` varchar(255) NULL DEFAULT NULL
    );
    

     

    The following insert would set location to NULL and id to the next auto_increment value:

     

    INSERT INTO people (name, type) VALUES ('Albert', 'Cool');
    

     

    NOTE: I am pretty sure that varchar has a max length of 255 - varchar(500) is likely to be trimmed anyway but is also misleading.

  2. Consider using a join, something like:

     

    DELETE customer_tb, customer_site_notes 
    FROM customer_tb
         LEFT JOIN customer_site_notes ON customer_tb.id = customer_site_notes.id
    WHERE customer_tb.name = '$delcust';
    

     

    Use an INNER JOIN with an enforced 1:1 relationship between customer_tb and customer_site_notes.

  3. Consider altering the table definition if at all possible:

     

    CREATE TABLE member (
         id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
         name VARCHAR(100) NOT NULL DEFAULT 'undefined'
    );
    
    CREATE TABLE subscription (
         id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
         member_id INT(10) UNSIGNED NOT NULL DEFAULT 0,
         start_date DATE NOT NULL DEFAULT '0000-00-00',
         end_date DATE NULL DEFAULT NULL,
         INDEX FK_MEMBER_ID (member_id)
    );
    [code]
    
    Then something like this would probably be wrong:
    
    [code]
    SELECT member.id, member.name, latest_subscription.start_date, latest_subscription.end_date
    FROM member
         INNER JOIN ( SELECT * FROM subscription ORDER BY start_date DESC ) AS latest_subscription ON member.id = member_id
    GROUP BY member.id;
    

     

    Where as this would be just wacky:

     

    SELECT member.id, member.name, latest_subscription.start_date, latest_subscription.end_date
    FROM member
         INNER JOIN ( 
              SELECT * FROM subscription INNER JOIN ( 
                   SELECT member_id, max(start_date) AS max_start_date FROM subscription GROUP BY member_id 
              ) AS max_subscription ON subscription.member_id = max_subscription.member_id AND subscription.start_date = max_subscription.max_start_date
         ) AS latest_subscription ON member.id = latest_subscription.member_id;
    

    [/code][/code]

  4. Assuming that DB1 and DB2 are not actually databases and refer to tables, I do not see any SQL that joins DB1 and DB2, something like:

     

    SELECT d1.*, d2.citynameinenglish
    FROM DB1 d1, DB2 d2
    WHERE d1.cityid = d2.cityid
    
    OR
    
    SELECT d1.*, d2.citynameinenglish
    FROM DB1 d1
         INNER JOIN DB2 d2 ON d1.cityid = d2.cityid
    

     

    FYI: db2 should be d2 in the SELECT clause of your query, boko605.

  5. I performed some tests that produced some interesting results:

    <?php
    for ($i = 0; $i < 500000; $i++)
    {
         $_POST['value'] = " ";
    
         preg_match('/^\s*$/', $_POST['value']) ? $_POST['value'] = "value" : $_POST['value'] = $_POST['value'];
    }
    
    SCRIPT END - Script Execution Time: 1.4779 Seconds 
    SCRIPT END - Script Execution Time: 1.4797 Seconds 
    SCRIPT END - Script Execution Time: 1.4729 Seconds 
    
    for ($i = 0; $i < 500000; $i++)
    {
         $_POST['value'] = " ";
    
         $_POST['value'] = preg_match('/^\s*$/', $_POST['value']) ? "value" : $_POST['value'];
    }
    
    SCRIPT END - Script Execution Time: 1.4364 Seconds
    SCRIPT END - Script Execution Time: 1.4353 Seconds  
    SCRIPT END - Script Execution Time: 1.4458 Seconds
    
    for ($i = 0; $i < 500000; $i++)
    {
         $_POST['value'] = " ";
    
         if (preg_match('/^\s*$/', $_POST['value'])) // TRUE
         {
              $_POST['value'] = "value";
         }
         else
         {
              $_POST['value'] = $_POST['value'];
         }	
    }
    
    SCRIPT END - Script Execution Time: 1.3963 Seconds 
    SCRIPT END - Script Execution Time: 1.4059 Seconds 
    SCRIPT END - Script Execution Time: 1.4021 Seconds 
    
    
    for ($i = 0; $i < 500000; $i++)
    {
         $_POST['value'] = " ";
    
         if (preg_match('/^\s*$/', $_POST['value'])) // TRUE
         {
              $_POST['value'] = $_POST['value'];
         }
    }
    
    
    SCRIPT END - Script Execution Time: 1.4746 Seconds 
    SCRIPT END - Script Execution Time: 1.4774 Seconds 
    SCRIPT END - Script Execution Time: 1.4808 Seconds 
    ?>
    

     

    The results for the last two tests are quiet contradictory, could someone please verify the last two tests!! - The test result suggests that adding a superfluous else to an if would decrease the processing time.

     

    I may have to stop using ternary operators so often.

  6. There is a limitation if only one value out of the entire array requires html entities to be converted.

     

    ... and use get_magic_quotes_gpc internally, if that is the only condition used for stipslashes.

     

    I prefer prepareing post data for database insertion in a couple of seperate steps.

     

    First just lightly clean up the data

     

    foreach ($_POST as $key => $item) $_POST[$key] = trim( get_magic_quotes_gpc() ? stripslashes($item) : $item );
    

     

    ... and then the more complex type validation that includes string quoting or returns NULL;

     

    mysql_query($query = "INSERT INTO table SET value = ".to_string($_POST['value']);
    
    // return an escaped and quoted (if required html entity converted) value or null
    function to_string($value, $htmlentities=false)
    {
         return is_null($value) || empty($value) ? "NULL" : "'".mysql_real_escape_string( $htmlentities ? htmlentities($value) : $value )."'";
    }
    

     

    NOTE: The above function can not be used to convert numbers since empty(0) returns true.

  7. SELECT network.nid
        , network.userid
        , network.friendid
        , if (network.userid = 1, friend.username, owner.username) AS username
        , if (network.userid = 1, friend.main_image, owner.main_image) AS main_image
        , if (network.userid = 1, friend.level, owner.level) AS level
    FROM network
        INNER JOIN users AS owner ON network.userid = owner.userid
        INNER JOIN users AS friend ON network.friendid = friend.userid 
    WHERE ( network.userid = 1 OR network.friendid = 1 ) AND ap = 1;
    

     

    SELECT network.nid
        , network.userid
        , network.friendid
        , if (network.userid = $userid, friend.username, owner.username) AS username
        , if (network.userid = $userid, friend.main_image, owner.main_image) AS main_image
        , if (network.userid = $userid, friend.level, owner.level) AS level
    FROM network
        INNER JOIN users AS owner ON network.userid = owner.userid
        INNER JOIN users AS friend ON network.friendid = friend.userid 
    WHERE ( network.userid = $userid OR network.friendid = $userid ) AND ap = $ap;
    

     

    EDIT: though the query looks a lot different the primary problem was network.userid in the second LEFT JOIN should be network.friendid and I figured an INNER JOIN is more appropriate since both ids are required.

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