Results 1 to 5 of 5

Thread: mysql_pconnect or mysql_connect ?

  1. #1
    Join Date
    Jun 2006

    mysql_pconnect or mysql_connect ?

    I hope someone can answer these questions.

    I've done some research and decided to use mysql_pconnect for the following reasons.

    I've written my own CMS using objects in PHP. All the objects are in one file along with the connection function. (Please advise if seperate files are recommended). The objects file are included in most pages. Most functions within objects connect to the database but do not close the connection. Normally, I'd think this is a bad thing but if mysql_pconnect is being used, do I have to close the connect in each function?

    Please correct me if I'm wrong; the mysql_pconnect simply looks for an existing connection and uses the same one virtually using only one connection.

    Any help would be much apprecaited.

  2. #2
    Join Date
    Aug 2004
    You might want to read this PHP manual page: http://www.php.net/manual/en/feature...onnections.php

    Whether or not you opt to use persistent connections, I would not recommend having each class that needs DB access doing its own connection. Instead, I would have one database connection class, instantiate it to create the connection, then pass that object to each other object's constructor that needs it. If you use MySQLi instead of the older MySQL extension, you can just instantiate a MySQLi object and pass that to each applicable object.

    PHP Code:
    class Foo
       public function 
    __construct(MySQLi $db)
    $this->db $db;
       public function 
    $sql "SELECT * FROM table WHERE id=" . (int) $id;
    $result $this->db->query($sql);

    // USAGE:
    $db = new mysqli($dbHost$dbUser$dbPass$dbName);
    $foo = new Foo($db);
    $data $foo->bar(1);
    Last edited by NogDog; 10-01-2010 at 01:50 PM.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jun 2006
    Thanks for responding NogDog.

    That is what I've done. Maybe an example would help explain.

    I created a connection class that accepts values in order to make the connection.

    Connection Class:
    class db_connection{
                // database connection
                 function db_con($host,$name,$password){
                     mysql_pconnect("$host","$name","$password") or die(mysql_error());
    Then I've created other classes with function to actually do the work.

    //load selected blog title into editor    
            function editor_show_post_tle(){
                        $qry_edit_title="SELECT * FROM blog WHERE blog_title='$pg'";
                            echo $row_page_title["blog_title"];
              // should there be a close connection here?
    As you can see the connection has been made ( providing the values are sent to the variables) and the function doesn't have a mysql_close() within it.

    Imagine many functions in the same manner with not mysql_close(). Would this cause an issue and should I be closing the database within each function?

  4. #4
    Join Date
    Aug 2004
    No, you do not want to close the connection as part of the query function, as any subsequent queries would then fail unless you then did another mysql_[p]connect() first. The persistent aspect of the pconnect only affects subsequent calls to mysql_pconnect(), not calls to any other mysql_*() function. In fact, if you do close it, then there is no reason to use mysql_pconnect(), as a closed connection is a closed connection, regardless of whether it was persistent.

    PS: I've generally avoided persistent connections, as they can become problematic if anything is not configured just right, with orphaned connections using up your allowed limit of connections. They can make a performance difference in some cases, and you can always implement them later if you determine you need to find some performance boosts and are willing to keep an eye on things and tweak web server and database server settings to address any problems that arise. Generally, application design and database/query optimization will do much more to improve performance than using mysql_pconnect() once per script instead of mysql_connect() once per script.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Jun 2006

    Thanks for all your help

    Thanks NogDog for all your help. I appreciate your time.

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10891 seconds
  • Memory Usage 2,899KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates