Results 1 to 6 of 6

Thread: mysql connection...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    74

    mysql connection...

    helo
    I have 2 question about efficiency of code:
    1. in my codes I have a file that responsible to the mysql connection which I always include in every file that I want to connect the database in.

    this is the file:

    Code:
        $mysql_link=mysql_connect('xxxxxxx','xxxx','*******') or die("ERROR: cannot connect to MySQL server.");
    	if (!mysql_select_db(xxxxx',$mysql_link))
    		echo "Could not select the DB.";
    now, in every page that I have mysql queries, I do the queries like this:

    Code:
    $result=mysql_query("SELECT * FROM xxxx WHERE xxxx='$xxx' ORDER BY xxxx");
    is this ok?
    or should I do the queries like this:
    Code:
    $result=mysql_query("SELECT * FROM xxxxxxx WHERE xxxx='$xxx' ORDER BY xxxxx",$mysql_link);

    2. should I close the connection to the mysql in every page? because now I dont close it and its working fine...
    but maybe its not good to the server?

    Yair
    -------------------------------------
    http://www.ybweb.com

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Your first type of query seems to be fine. I use MS SQL server and I do it exactly the same, except that it's "mssql_query" instead of "mysql_query".

    I also don't close the connection to the server after queries are run and it works fine.

    I guess I'm interested to know if that's bad or not also.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    The only time you really need to close the connection is if you have tons of code executing after it, or you connect to other databases, or something weird.

    When the code finishes executing and output is sent to the browser, the connection is closed automatically.

    So unless you finish with the database and have a lot to do after that and want to free up some memory, you shouldn't need to close it.

    From the manual itself:

    And the only time you would need to do:

    Code:
    $result=mysql_query(sql, $mysql_link);
    Is when you have more than one database connection opened. Otherwise it's okay to leave it off. Without it, it's just assumed to go to the last opened database.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I decided to double check the manual incase I was wrong.

    mysql_query(string query [, resource link_identifier])
    If link_identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mysql_connect() was called with no arguments, and use it.
    mysql_close([resource link_identifier]))
    Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.
    So, yeah.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    74

    what a relief to know that :)

    I am alittle bit ashamed that I was too lazy to look in the manual myself...

    thank you

    Yair
    -------------------------------------
    http://www.ybweb.com

  6. #6
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I was going to also add, that when you watch the profiler for MS SQL Server, it sends a disconnect after each query, so that is already being done for you. I'm sure it's the same for MySQL also.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

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



Click Here to Expand Forum to Full Width