Making mysql_connect more efficient in my script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    Making mysql_connect more efficient in my script

    I'm working on some scripts someone else made, and it's littered with
    repetitive mysql_connect's and mysql_select_db 's.
    For example:
    // MAKE SURE IT IS A VALID PRODUCT
    $connection2 = mysql_connect(" localhost", "root") or die ("Couldn't
    connect to the server.");
    $db2 = mysql_select_db ("priceretai l", $connection2) or die ("Couldn't
    select database.");
    $sql2 = "SELECT dbname FROM productlist WHERE ((accountid =
    'printingautoma tion') OR (accountid = '$accountid')) AND dbname =
    '$sel_typeprodu ct'";

    --SNIP--

    // MAKE SURE IT IS A PRODUCT THAT HAS PRICING SET FOR IT
    $connection3 = mysql_connect(" localhost", "root") or die ("Couldn't
    connect to the server.");
    $db3 = mysql_select_db ("priceretai l", $connection3) or die ("Couldn't
    select database.");
    $sql3 = "SELECT companyid FROM $sel_typeproduc t WHERE companyid
    --SNIP--

    Yes, it DOES say root in the script.
    Here's what I'm wanting to do: make a script that contains the two
    mysql_connect and mysql_select_db lines and then do a require() or
    require_once() to call it. (And set up and use a different account
    than root.)

    Now, the questions:
    Should I use require or require_once?
    And is there a reason why the connection is established multiple times
    that I don't understand? Can't it be established once at the top and
    use new mysql_query's each time?

    Thanks for any help!!
    Liam
  • Marcin Dobrucki

    #2
    Re: Making mysql_connect more efficient in my script

    LRW wrote:
    [color=blue]
    > And is there a reason why the connection is established multiple times
    > that I don't understand? Can't it be established once at the top and
    > use new mysql_query's each time?[/color]

    Once you open a connection to MySQL, it will presist until the script
    terminates, so all you should need to do is to open it once, and then
    just do mysql_query on that connection.

    If you look at the mysql function docs on the php.net site, you will
    also notice that somewhere they talk about creating presistant
    connections that last between scripts. It's useful when you have loads
    of scripts and you are getting a serious overhead from the connection
    being opened every time a script is run.

    /Marcin

    Comment

    Working...