Support Forums

Full Version: [TUT] Include mySQL into php.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Make a settings.inc.php and put this in there:

(CHMOD should be: 755)
PHP Code:
<?
        $CONFIG
['DBHOST'] = "localhost"// Database servers location, normally localhost
        
$CONFIG['DBUSER'] = "username"// sql user
        
$CONFIG['DBPASS'] = "password"// db users password
        
$CONFIG['DBNAME'] = "database"// Database to connect to
?>

then make database.php

(CHMOD should be: 644)
PHP Code:
<?
        
include("settings.inc.php");
        
$connection mysql_connect($CONFIG['DBHOST'],
        
$CONFIG['DBUSER'],
        
$CONFIG['DBPASS']) or die("Could not establish a connection with mysql.");
        
mysql_select_db($CONFIG['DBNAME']) or die("Unable to select database.");
?>

Then in all your codes put:

PHP Code:
<? include("database.php"); ?>

You are done Big Grin


Note: If you need help with CHMOD visit this link: http://www.supportforums.net/showthread.php?tid=182
Thats simple enough,
So, what CHMOD should implement on both files to securing? Smile
Good idea, I shall add that into the tut once I get home.


EDIT: added it.
Hmm... Shorthand? Yuck.
Heres how I connect to my databases

Code:
<?php
class connection {
    private $database, $connect;
    function connect() {
        $this->database = array(
        'username' => 'USERNAME',
        'password' => 'PASSWORD',
        'host' => 'localhost',
        'name' => 'DATABASE NAME'
        );

        $this->connect = mysql_connect($this->database['host'], $this->database['username'], $this->database['password']);
        mysql_select_db($this->database['name'], $this->connect);
    }
}

$db = new connection();
$db->connect();
?>
(10-05-2009, 05:30 PM)xiofire Wrote: [ -> ]Hmm... Shorthand? Yuck.
Heres how I connect to my databases

Code:
<?php
class connection {
    private $database, $connect;
    function connect() {
        $this->database = array(
        'username' => 'USERNAME',
        'password' => 'PASSWORD',
        'host' => 'localhost',
        'name' => 'DATABASE NAME'
        );

        $this->connect = mysql_connect($this->database['host'], $this->database['username'], $this->database['password']);
        mysql_select_db($this->database['name'], $this->connect);
    }
}

$db = new connection();
$db->connect();
?>

Correct, simple and easy enough.. Good work ;)
(10-06-2009, 08:48 AM)immi Wrote: [ -> ]Correct, simple and easy enough.. Good work ;)
Thank ya, I take pride in my PHP Tongue
Both methods work, nice tutorial.
Thanks for the great tutorial, worked well.
yeah both methods work
Very nice TUT, this will help out alot of members
Pages: 1 2 3 4 5