|
-
Feb 26th, 2004, 10:31 AM
#1
Thread Starter
Lively Member
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
-
Feb 26th, 2004, 10:38 AM
#2
Frenzied Member
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.
-
Feb 26th, 2004, 12:10 PM
#3
Stuck in the 80s
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.
-
Feb 26th, 2004, 12:12 PM
#4
Stuck in the 80s
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.
-
Feb 26th, 2004, 12:58 PM
#5
Thread Starter
Lively Member
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
-
Feb 26th, 2004, 01:36 PM
#6
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|