Announcement

Collapse
No announcement yet.

Certain Data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Certain Data

    I'm trying to cut down on the number of mysql querys, and i was wondering if there was a way to get all the data from a table, close the connection, then just display a certain piece from a certain id

  • #2
    I don't think so.

    You would have to remove features for this to happen.

    Why would you want to decrease query's ?
    Email: [email protected]
    Site: Under Construction

    Comment


    • #3
      oops, I mean't connections. I'm trying to get my script to optimum performance

      Comment


      • #4
        The only way to decrease connections is too decrease the amount of people who use your board.

        Or program is script that only allows X amount of people to browse the board at once.
        Email: [email protected]
        Site: Under Construction

        Comment


        • #5
          i only just start php at the end of august, i'm modifying old scripts, they had multiple open and close databases, and if I am using a display lots of data type of thing, then it would take ages before it get to the bottom of the script and closes the connection

          Comment


          • #6
            Try having one connect command at the beginning and one close at the end.

            You may expericence timeouts.
            Email: [email protected]
            Site: Under Construction

            Comment


            • #7
              hmm, what if i made it cycle through all the data, and when the id is equal to the id i want it will allow it to display, it might work

              Comment


              • #8
                Try it.
                Email: [email protected]
                Site: Under Construction

                Comment


                • #9
                  It works, but its much slower than it was before. There has got to be a way to add a WHERE id=$id command into the mysql_fetch_array, or some other code for collecting

                  Comment


                  • #10
                    As far as your problem goes....
                    if you know the ID, and you still have a DB connection, you can use mysql_data_seek(). If you want to connect, query, then disconnect real fast, I'd say throw the ID into the query's WHERE clause.

                    If you want to query everything, and disconnect, yet still have all your results, you can load the whole recordset into an array. And this is where that speed vs. space comes into play. An array full of a recordset is a huge memory hog. But at least you don't have the access time that you would have if you were using the mysql fuctions. Generally speeking, this is not a good idea. Unless your recordset is very small, never load it into an array. The time gained from using an array (as opposed to a mySQL recordset) is so small, you may loose it by take up a large portion of the system RAM (thus slowing the entire machine).



                    Just my little lecture of the moment.

                    When programming there are a few scales to weigh. For example:
                    speed vs. space
                    developement time vs. code efficiency

                    for speed vs. space....
                    Quieries execute much faster on a DB that is not normalized. That is to say, tables that duplicate data. A normalized DB would strip out all duplicate data and use foreign keys to link data together with relationships. But by having the duplicate data, you use up space.

                    speed vs. space can also be a big issue when it comes to memory. Let's assume you have a mathimatical value that is derived through some computation, and this value is used several times throughout script execution. How would you decide whether to compute this value each time it's needed, or whether to compute it once and store it in memory. Well, you would most likely look at the time it takes to derive the value against the amount of space it takes up in memory.

                    for developement time vs code efficiency....
                    It takes time to optimize code. If your in a hurry and code efficiency is not an issue, you can develope a piece of junk rather quickly. But if you require code to be efficient in it's use of memory space and processor time, it will take much more developement time to write.

                    Comment


                    • #11
                      Thanks Max Albert, I never thought about it all like that

                      Comment


                      • #12
                        You can draw it out like you would not believe.

                        I see a lot of this:
                        PHP Code:
                        $some_flag "down";
                        for (
                        $i=0$i<=count($some_array); $i++) {
                            if (
                        $some_flag == "up") {
                                echo 
                        $some_array[$i];
                            }
                            else {
                                echo 
                        $some_array[$i] . $something_else;
                            }

                        Do you see anything wrong with this? The IF statement is evaluated inside the loop even though it's value will not change within the loop. I saw this just about 3 hours ago in a script I was hacking for a client. I promptly changed it to look like this:
                        PHP Code:
                        $some_flag "down";

                        if (
                        $some_flag == "up") {
                            for (
                        $i=0$i<=count($some_array); $i++) {
                                echo 
                        $some_array[$i];
                            }
                        }
                        else {
                            for (
                        $i=0$i<=count($some_array); $i++) {
                                echo 
                        $some_array[$i] . $something_else;
                            }

                        Now, the IF statement is being evaluated only once. It does take up a few more bytes in your file, but look at the dramatic speed difference. The time difference is directly proportional to the size of $some_flag.


                        Another example:
                        PHP Code:
                        $first FALSE;
                        foreach (
                        $thingy as $tmp) {
                            if (
                        $first) {
                                
                        $first FALSE;
                            }
                            else {
                                
                        $myFields .= ", ";
                            }
                            
                        $myFields .= $tmp;

                        When dynamically building queries, people like to use a loop and append the fieldname or where clause parameter. And in doing this, they often need to nest an IF statement to determine if this is the first or if they need to append a comma. DON'T. This is much easier to read, and easier to write:
                        PHP Code:
                        foreach ($thingy as $tmp) {
                            
                        $myFields[] = $tmp;
                        }
                        $myFields join(", "$myFields); 
                        Most sloppy code is just sloppy code. The time or space lost doesn't really matter. Today's hardware can take the punishment. I mean, processors are now in the GHz, and some servers are multi-processor. RAM is real cheap right now (except my RD RAM ) that servers can buy 1GB of PC133 SD RAM for under $150. Hard Drives are real cheap now too. I have a 40GB I got for $90 on sale somewhere. I also have a 30GB and a 13GB, and I'm just a Joe Schmoe with some home computers. Can you imagine what hosting companies could be running if they weren't money hungry? Good golly!!

                        It's 2AM, I'm sleep deprived, and my mind stopped a while ago. I'm giong to shut up now.

                        P.S. - Happy Thanksgiving! Where's my turkey!!

                        Comment

                        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                        Working...
                        X