Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: mysql_connect

  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    97
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mysql_connect

    how often should i call this function??

    should i call it at the very beginning and use it throughout all the include()'ed pages?

    or (as i did before) use mysql_connect for every single function?
    Edit: Nothing that can be considered offensive in the signature, please. // liorean

    www.moresense.co.uk

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alexpayne
    how often should i call this function??

    should i call it at the very beginning and use it throughout all the include()'ed pages?

    or (as i did before) use mysql_connect for every single function?
    I just make a file & require() the file whenever I have to communicate with the database. Example file below:

    PHP Code:
    <?php
    $user_name 
    "username";            //DB user name
    $pass_name "password";            //DB password
    $db_name "databasename";          //DB Name

    /** I use this in all query strings so in case I need to rename a table 
    I don't have to change the table name in all my files. I just change 
    it here 1 time & not every page in my site **/
    $table_one "table_one_name";        //Orders Table
    $table_two "table_two_name";        //Customers Table
    $table_three "table_three_name";    //Referral System Table


    //this connects to the database
    $conn mysql_pconnect("localhost""$user_name""$pass_name") or die(mysql_error());

    //this selected the dabase. The DB is defined above with $db_name
    $db_selected mysql_select_db("$db_name"$conn) or die(mysql_error());

    //this will display if the connection was made successfully
    if (!$db_selected) {

    //this is the text that displays if the DB connection wasn't made!
       
    die('Could not connect to database: ' mysql_error());
    }

    //this is the text that displays if the DB connection was made!
    echo '';
    ?>

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You should be connecting as little as possible. If you're only using one database, connect at most once per script. Dont' disconnect until the end of the script.

  • #4
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    You should be connecting as little as possible. If you're only using one database, connect at most once per script. Dont' disconnect until the end of the script.
    Is the once per script a security issue or a bandwidth/resources issue? I just call my connection script only when I have to connect...I just did this since I thought it would use less bandwidth/resources.

  • #5
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    it is a bandwidth/usage issue. normally the number of connections you have to your database is limited. if you have a site with numerous users opening 5 or 10 connections at a time you can find yourself running out of connections, and users not able to connect to your site.

    it is also resource intensive if you have to continually reconnect to the database.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it is a bandwidth/usage issue

    How can you use bandwidth when connecting to localhost?

    ~Phil~

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you have more than one in a single script, it won't use up all your db connections. Subsequent mysql_connect() calls will just return the existing connection instead of making a new one unless you specifically force it to.

    Of course, this means all those extra calls are just eating resources for nothing, though, so you should still get rid of them.


  •  

    Posting Permissions

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