www.webdeveloper.com
Results 1 to 6 of 6

Thread: Please Could you check my code if its the right way

  1. #1
    Join Date
    Jun 2007
    Location
    England
    Posts
    448

    Lightbulb Please Could you check my code if its the right way

    Hi everyone hopefully my code bellow does the following, but wounderd if this is the correct way to go through each row and update each user
    PHP Code:
    <?

    //connect to building database
    $result mysql_query("SELECT * FROM buildup"
    or die(
    mysql_error());  
    $row mysql_fetch_array$result )

    //some variables of time                
    $expiry $row["endtime"];
    $now time();
    $upgrade='bank';
    $buildingnumber $upgrade

    //if endtime has passed time update there building
    if($expiry $now){
                                      
    mysql_query("UPDATE users SET $buildingnumber=$buildingnumber+'1'");
                                        echo 
    "Updated building Successfully";
                
    mysql_query("DELETE FROM buildup WHERE $expiry < $now");
                    echo 
    "Deleted and updated Successfully";

    elseif(
    $expiry $now){
                                  echo 
    "Nothing to update";
    }else {
                                   echo 
    "Error! Thats not good";
    }


    ?>
    is this the best efficant way of doing this, would this go through each row and do the above.

    thanks to anyone that helps

  2. #2
    Join Date
    Sep 2006
    Posts
    655
    One suggestion, if you are not using any variables in your echo statement then you should use ' ' instead of " "

    Wrong way:
    echo "Nothing to update";

    Correct way:
    echo 'Nothing to update';


    This way you can make your code much faster since PHP looks for variables within "" and does not in ''

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Quote Originally Posted by cancer10
    One suggestion, if you are not using any variables in your echo statement then you should use ' ' instead of " "
    Actually, if you do some testing, you'll find there is essentially no difference as long as there are, in fact, no variables to be interpolated within the string. Where you do get some minor time savings (we're talking milli- or more likely micro-seconds here) is using concatenation instead of interpolated variables within the string, .e.g.:
    PHP Code:
    echo "This " $is " faster.";
    echo 
    "This $is slower.";
    echo 
    'This ' $is ' probably the same as the first one.'
    Last edited by NogDog; 05-25-2008 at 04:09 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Your code is missing a couple semi-colons, so as it is now it won't even parse.

    Not knowing what your functional requirements are for this code, I'm not sure whether it really does what you want. It appears to retrieve all records from the "buildup" table, but it only reads one row from the result set.

    PS: You should always use the "<?php" opening tag instead of "<?", in order to ensure maximum compatibility, avoid confusion with <?xml> tags, and be forward compatible with PHP 6 (which will not even have an option to allow the short tag).
    Last edited by NogDog; 05-25-2008 at 04:11 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2007
    Location
    England
    Posts
    448

    Exclamation

    i want this to be carried out on each result so do i need to do
    PHP Code:
    while() 
    ?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    The typical idiom with the mysql functions is:
    PHP Code:
    $result mysql_query($query);
    while(
    $row mysql_fetch_assoc($result))
    {
       
    // do stuff here with the elements of the $row array

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles