Page 1 of 1

MySQL Connect

Posted: Tue Feb 04, 2003 2:51 pm
by icesolid
When I connect to my MySQL server I just do it like this:

Code: Select all

<?php
include("connect.php");

$result = mysql_query("SELECT * FROM table ORDER BY id ASC");
$row = mysql_fetch_array($result);
?>
The connect.php file contains:

Code: Select all

<?php
$link = mysql_connect("localhost", "username", "xxx") or die("Could not connect");
mysql_select_db("db_name") or die("Could not select database");
?>
Should I also add after each opening of the database:

Code: Select all

<?php
mysql_close($link);
?>
And can you give me any other ways of doing this. I am looking for a more secure way of connecting so after each opening the database is closed and there is no way of getting in. Just a security question.

Thanks!

Posted: Tue Feb 04, 2003 3:05 pm
by volka
when the script ends (after each request) exisiting/open mysql-connections are closed (see also: http://www.php.net/manual/en/function.m ... onnect.php for the opposite behaviour).
The advantage of a file like your connect.php is that you can place it outside the tree that is reachable from the web. So even if the php-processor is shutdown (why ever) no passwords are exposed to the world.