SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face How many times do you mysql_connect()?

    I'm just trying to get an understanding of how professionals do this:

    Code:
    ----------------begin info.inc------------------------------
    // all the appropriate info dor host, account, password, etc.
    $connect = mysql_connect($host,$account,$password);
    $db = mysql_select_db($dbname,$connect);
    ----------------end info.inc-------------------------------
    
    ----------------begin mypage.php-------------------------
    include("info.inc");
    include("myClass.php");
    // get poll info from db
    $sql = "select someinfo,someotherinfo from mytable where pollID = " . $pid;
    $result = mysql_query($sql,$connect);
    $rows = mysql_fetch_array($result);
    //use row data somehow
    $myVar = new myClass();
    $myVar->myFunction();
    ---------------end mypage.php---------------------------
    
    ---------------begin myClass.php------------------------
    //usual contructor and other memberVar and such...
    function calculate()
    {
        include("info.inc"); // <---PAY ATTENTION TO THIS LINE
        $sql = "select somenewinfo,someotherinfo from mytable where pollID = " . $pid;
        $result = mysql_query($sql,$connect);
        $rows = mysql_fetch_array($result);
    }
    ---------------end myClass.php---------------------------
    in the calculate() function, i have to include the info.inc file in order to talk to the database within the function. Is this what professionals do? or do they pass the $connect and $db by reference? how many queries do people limit their pages at? let's say i wanted to do 10 queries on one page, is this possible without slowing down the server? Keep in mind I may have 100 or more people using the page at one time.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use include_once instead of include the file will only be included once. That'll solve your problem.
    You don't really need the connection_id ($connect) if you only use one database per script (and most likely you don't).
    Oh - and mysql_select_db doesn't return anything useful really, so no need for the $db variable either.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    so what about the multiple queries in one page?

    is it common practice to have multiple queries on one page?

  4. #4
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, if your using forums, you usually have 5-6.

  5. #5
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    You don't really need the connection_id ($connect) if you only use one database per script (and most likely you don't)
    If data access code is picking up an old, previously created connection that could cause problems in tests with connection state like errors, currently used db, affected rows, etc persisting from one test method to the next.

    Php is desperate to make connections global. Quite apart from the optional connection parameter, if you mysql_connect with the same server/user/pass as in a previous call, you'll get the previously created connection. The only solution is to use the optional new link parameter. In scripts, pass a db object around if you want to share the conn.

    PHP Code:
      class MysqlDatabase // implements Database
      
    {
          var 
    $failed_conn_error 'Failed to connect to database server [%s].';
          var 
    $_connection;       // (resource)
      
           
    function MysqlDatabase($server$user$password)
           {
               
    $this->_connection mysql_connect($server$user$passwordtrue);
           }
      
      
    // etc 

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One More Question..

    So include_once makes those variables that are included global?

  7. #7
    SitePoint Addict jough's Avatar
    Join Date
    Sep 2001
    Location
    You have moved into a dark place. It is pitch blac
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How many queries should you have on a page?

    As many (or as few) as you need to do the job you want to do. You shouldn't have too many, or too few - queries should be like the porridge that Goldilocks ate: just right.

    It is good practise to put your DB info into ONE file (connect.php, db.php, whatever, but if you're going to use an extension other than PHP make sure it's not in your public_html directory, otherwise someone could just read it as plain text).

    Use vars for the DB selection if you're going to have more than one DB called in your scripts. Otherwise you don't need to assign the function to a handler.

    Also, no matter how many queries you have on a page (or rather in a single runtime script) you should only include the connection file ONCE:

    Code:
    include_once ("path/to/connection.php");
    Then you're good to go. And it may make sense to close the connection when you're done, at the bottom of the script:

    mysql_close();

    The connections will close (eventually) after the end of the script, but it's better to get into the habit of cleaning up after yourself. Even with automatic garbage collection it's good to be in the habit of freeing up memory whenever possible.
    My Dynamic Site in Action:
    Poetry X Poetry Archive

  8. #8
    SitePoint Addict jough's Avatar
    Join Date
    Sep 2001
    Location
    You have moved into a dark place. It is pitch blac
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrwatkin
    So include_once makes those variables that are included global?
    No, it just makes sure that the file is included only once.
    My Dynamic Site in Action:
    Poetry X Poetry Archive


Bookmarks

Posting Permissions

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