What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Help

PHP Help
Author: Message:
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP Help
A better way to do it:

code:
<?php
// Connection info
$database['username'] = "username";
$database['password'] = "pass";
$database['database'] = "database";
$database['host'] = "localhost";


// Database connect code

$database['link'] = false;
function query($query) {
   global $database;
   
   if(!$database['link']) {
      make_connection();
   }
   return mysql_query($query, $database['link']);
}

function make_connection() {
   global $database, $talen;

   $database['link'] = mysql_connect($database['host'], $database['username'], $database['password']);
   
   if(!$database['link']) {
      die("Could not connect to server " . $database['host']);
   }
   if(!mysql_select_db($database['database'], $database['link'])) {
      die("Database \"" . $database['database'] . "\" wasn't found!");
   }
}
?>

Include this in all your scripts (I always make an include file in which I include everything) and when you need to acces the database just do:
code:
$result = query("INSERT INTO ...");

The code will never make more then 1 database connection, wil never make a connection when you don't need it, AND it works with multiple connections.

Just be sure you doesn't use $database somewhere else in the script, and off course use query instead of mysql_query everytime.

Besides that, you can better use require instead of include, because you're sure your scripts need the database... So if the file doesn't exist your script doesn't need to be executed.
11-21-2005 09:15 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
PHP Help - by DragonX on 11-21-2005 at 05:41 AM
RE: PHP Help - by ipab on 11-21-2005 at 06:57 AM
RE: PHP Help - by Ezra on 11-21-2005 at 07:49 AM
RE: PHP Help - by J-Thread on 11-21-2005 at 09:15 AM
RE: PHP Help - by DragonX on 11-21-2005 at 09:15 AM
RE: PHP Help - by ipab on 11-21-2005 at 04:37 PM
RE: PHP Help - by J-Thread on 11-21-2005 at 05:29 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On