MySQL Connect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

MySQL Connect

Post 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!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply