Question:
database connections in php?
dmurray
2008-02-03 11:00:13 UTC
I'm newer to PHP and developing a website using mySQL. I have a question regaring mysql_connect function.

Before I was only using mysql_connect for one query but now I added another module above the previous one to to show a list of the most recently joined users to the community. It appears that if I use close($cxn); in the first module the php script stops running past that point so only like 1/2 of my page shows up.

I took close($cxn) out and it works. I am a fairly experienced programmer and feel like leaving connections open is bad practice since I open a new one for the module after the new users.

What is causing this? How can I create modules that each open their own connection to the database? Without disrupting eachother.
Four answers:
badoob
2008-02-06 22:28:31 UTC
I would have to disagree with David; while it may not be necessary to close the non-persistent connection, it seems... sloppy; I would prefer to at least close the connection manually at the end of the script.



It looks like you are experiencing a problem of variable scope. If I understand you correctly, you are using include() statements which call scripts that independently open and close connections during their operation. If this is correct, the connection resource you create will overwrite any identically-named resource link identifier.



You might try changing the name of the resource in the different scripts to see if this is indeed the problem (that is, if you call , then include() a script which calls mysql_close($cxn);, $cxn will not be available to subsequent scripts unless it is reconnected) .



You might get around this by creating the connection early in the main script and terminating it at the exit point.



Slightly off-topic, being an experienced programmer you're certainly familiar with the expense and inherent risks involved with multiple database queries (php doesn't seem to provide much as far as built-in mitigation of such risks). You might benefit from memcached. Just a thought!
David S
2008-02-03 11:09:32 UTC
If I understand your question correctly, use mysql_close($cxn) instead of close($cxn).



Also, php will automatically close the connection at the end of every script. Since the overhead is usually quite low to keep it open, I normally don't even bother with closing it:



"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."
Chandresh Upadhyay
2014-05-28 23:37:50 UTC
$con=mysql_connect("localhost","root","");

$db=mysql_select_db("");

$cmd=mysql_query("delete from .....");
?
2016-05-24 07:48:14 UTC
are u using linux? if u are, I'm not sure but i think there is a problem with mysql maybe u instal it 2 times or instal another database manager so I suggest u to use a sample to make sure that your mysql has no problem


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...