Jump to content

bibby

Members
  • Posts

    171
  • Joined

  • Last visited

    Never

Posts posted by bibby

  1. I'm going with yes. Each system should have it's own database, so google for how to manage multiple database connections in the same script, or experiment on your own using mysql_query()'s oft forgotten second parameter , the resource identifier. [url=http://us2.php.net/manual/en/function.mysql-query.php]http://us2.php.net/manual/en/function.mysql-query.php[/url].

    Each of these systems also have a query that adds a user. When that happens, add the user to the other two. There shouldn't be anything difficult about this, unless one day you pulled them apart.
  2. I'm a big proponent of using PHP to handle your conditionals rather than making a query handle it, but you could!

    MySQL IF statements take three arguments,  (condition, what to do if true , what to do if false)

    Say you had a table with people and their income, and you wanted to export them as either rich or poor. Try this:
    [code]
    SELECT
      userID user,
      IF (income>=50000, 'rich' , 'poor') status
    FROM
      users;
    [/code]
    Your export would have either have 'rich' or 'poor' for every user based on the number entered for income.


    Here's another scenario where we could mix columns during a select...
    Let's say there are users,  points earned, and the maximum allowable points.
    Naturally, we'd take the lowest of the two point columns since points earned cannot exceed the maximum (but for data collecting reasons we didn't want to overide the points in php). Try:

    [code]
    select
      userID,
      IF (pointsEarned > maxPointsAllowed , maxPointsAllowed, pointsEarned)
    from game;
    [/code]
    So if a record's pointsEarned is higher than the maximum, the maximum is used.
    If the pointsEarned is lower, than the pointsEarned is chosen.
    (if they are equal it doesn't really matter)
    If we had many users, the second column of our export could easily be a mix of pointsEarned for some, and maxPointsAllowed for others.

    You could easily nest IFs as well.
    Recently I was asked to collect time on a time scale rather than straight integers, but I insisted on collecting raw integers knowing that I could parse it out later (and change it if I needed to).

    [code]
    select userID,
      IF(
        mins > 5,
        IF(
            mins >10,
            IF(
              mins >15,
              'more than 15 mins',
              '10-15 mins'
              ),
            '5-10mins'
            ),
        'less than 5mins'
        ) time_spent
    from users;
    [/code]

    time_spent , even though they are stored as ints, are exported as timespans that meet at least one of these conditions.
    And that's fun with IFs.
  3. Users of PhpMyAdmin might have wondered what the deal is with the default query textarea reading
    [b]SELECT * from table WHERE 1[/b]

    1 is TRUE
    0 is FALSE

    How does this help?

    ------------------
    [b]WHERE 1[/b]

    For starters, for data to be returned, it should at the very least exist.
    [i]Where 1[/i] is a condition that demands that the data be true in order to be returned (without though, it is still implied).

    [code]
    #show me userIDs in users THAT EXIST and userID=1
    select userID from users where 1 and userID='1';
    # is the same as
    select userID from users where userID='1';
    [/code]

    When building complicated conditionals in your php script, you may find it difficult at times to put all of your [b]AND[/b]s in the right place. Typically I see coders using
    [b]WHERE this AND that AND theOther[/b]
    But what happens if one day you have to elimate that first condition ('this')?
    You may see an error near 'WHERE AND that'.

    The way around that is to satisfy the first condition from the start, and simply add 'AND' to the beginning of everything else.

    [code]
    select *
    from table
    where 1
    and this='this'
    and that='that'
    and theOther='theOther';
    [/code]

    If my conditions are php generated, and I have AND in front of each, I could eliminate any or all of them and still have my query run.

    ------------------
    [b]WHERE 0[/b]

    [code]select * from users where 0[/code]
    This query instantly returns false. "[i]Show me everything that isn't there[/i]".

    It's pretty funny, but it comes in handy if, by the same method above, you are constructing a series of [b]OR[/b]conditionals that may or may not be present at any given time.

    [code]
    select * from users
    where 0
    OR userID='1'
    OR userLastIP='127.0.0.1'
    OR userName='admin'

    #show me everything in users that DOES NOT EXIST -> OR meets these criteria.
    [/code]

    At any point in my script, I could decide to use any or none of the OR conditions and the query still runs.

    -----
    Side benefits:

    Copying table structure
    [code]
    create table cloneUsers
    select * from users
    where 0
    [/code]

    Copying structure and data
    [code]
    create table cloneUsers
    select * from users
    where 1
    #where 1 is optional here
    [/code]

    These days, I'm using more and more multi-lined queries. Instead of my scripts eliminating single conditions, I'm cutting them by hand. Where 1 or 0 helps me by allowing me to cut whole lines or blocks of lines, and still have the query run.
  4. I havn't had a question yet, although I can't stop trolling this forum. So instead, I have a time saving tip for all of you that insist on using [b]AS[/b] in your queries.
    You really don't need it.

    [code]
    select user_name as name from users;
    #would be the same as
    select user_name name from users;
    [/code]

    You can rename you tables the same way, but doing so only helps if you're using multiple tables
    [code]
    select
      u.user_name
    ,u.street_address
    ,s.score
    from
      users u
      ,really_dumb_and_long_table_name s
    [/code]

    If you rename fields and tables, [b]WHERE[/b] statements will accept your renamed table, but [b]NOT [/b] your renamed field. Renaming fields only effects the output.

    [code]
    select
      u.userID id
    ,u.user_name name
    ,u.street_address addy
    ,s.score
    from
      users u
      ,really_dumb_and_long_table_name s
    where 1
    and u.userID = s.userID

    # u.id = s.userID  will not work  (unknown column 'u.id')
    [/code]

    Renaming tables is also helpful if you ever want to join a table to itself, but identify them as separate objects. But most of the time I get really sick of retyping the whole table name again in large queries (hence the single letter aliases).

  5. Figured I'd mark this up for you.
    I didn't need a show()/hide() after all ; toggle() did both.

    [code]

    <script>
    function toggle(hot,id)
    {
    var a = document.getElementById(id).style.display;
    if (a != 'block')
    {
    document.getElementById(id).style.display = 'block';
    hot.innerHTML='[-]';
    }
    else
    {
    document.getElementById(id).style.display = 'none';
    hot.innerHTML='[+]';
    }

    }
    </script>

    <style>
    div {
    background-color:#C0FFEE;
    }

    .hidden {
    display:none;
    background-color:#ABCDEF;
    }

    #hotstuff{
    cursor:pointer;
    }
    </style>

    <div>
    <span id="hotstuff" onclick="toggle(this,'secret')">[+]</span> &lt;&mdash; expand this row.
    </div>
    <div class="hidden" id="secret">
    blah<br />blah<br />blah<br />blah<br />blah<br />
    </div>
    [/code]
  6. You shouldn't need an image or glyph.

    If the '+' is the [i]hot [/i] element, then it's likely in it's own div or span.
    So you could replace the character in that div with [b].innerHTML()[/b] .

    Make a function for show() and hide(),  but make a third for [b]toggle()[/b], so that you can use the same function for both actions.
  7. RegEx would verify that the string format is sound, and fopen would only work in read only mode.

    When I scrape external site files, I use file() or file_get_contents();

    [code]
    if (!$file_get_contents($url))
    {
        // then it's not real, or at least not accessible.
    }
    [/code]
  8. [code]
    while ($r=mysql_fetch_row($q))
    {
          $stash[$r['id']]=$r['banner'];
    }
    [/code]

    So now , whenever you want,  just say $stash[12].
    Once you consider looping through $stash, then you're looping through the result for a second time, which isn't efficent.

    All this being said, your query is using banner.id ,  are you sure you are expecting more than one row?
  9. If the other site has register globals on (allowing you to fake $_POSTs as $_GETs),
    by all means pass variables through query strings for remote logins or the like. That's how most phpBB sites get porno ads. :P

    I've been considering a javascript approach to something that I have to do this week on a system that I do not control.
    The plan (plan D, if you're wondering) is to

    include('http://remoteServer.com/file.html')  , and have php generated javascript fill in the form after it loads.

    The scenario is that they use a webform to enter data that they already know on the local system.
    So if I can't get the other company to play nice with me in database exchanges, then I'll write a robot to fill in their forms as they're accustomed to.  bastards.

  10. Anything you declare or use in your classses are considered globally accessible.
    In some instances though, you may want to process references to these objects so that you don't screw up the original.

    [code]
    $clone &= $filesys;
    doSomethingTo($clone);
    [/code]

    You don't mean SUPER GLOBALS do you?
    I'm not making any connection between your question and example data.


  11. I've commented in my issues with the first condition of your code.
    By all accounts though, you should be able to pass $_SESSION['user'] and get the result you'd like.

    [code]
    if (is_array($varinfo))
    {
    //wait...  it's key=>value ,  so $k=>$v , yeah?
    foreach ($varinfo as $v=>$k)
    {
    //varinfo id still an array
    $varinfo = strip_tags($varinfo);  //<-- shouldn't this be $v ? (or $k is you left it as is?)
    $varinfo = htmlspecialchars($varinfo, ENT_NOQUOTES); //<-also $v
    $varinfo = htmlentities($varinfo);  //<-also $v

    if (get_magic_quotes_gpc())  //  use  if (function_exists('get_magic_quotes_gpc'))
    {
    $varinfo = mysql_real_escape_string($varinfo); 
    // why are you testing for the existence of one function, then using another?
    // you should probably echo out a message in the event that it doesn't exist.

      } // end if

    } // end foreach
    }
    [/code]


    For the second part : go ahead and use mysql_real_escape_string  ,  it will add just the slashes it needs to get by. addslashes() is indescriminate.
×
×
  • 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.