Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] Include mySQL into php.
#1
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
Reply
#2
Thats simple enough,
So, what CHMOD should implement on both files to securing? Smile
Reply
#3
Good idea, I shall add that into the tut once I get home.


EDIT: added it.
Reply
#4
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();
?>
Reply
#5
(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 ;)
Reply
#6
(10-06-2009, 08:48 AM)immi Wrote: Correct, simple and easy enough.. Good work ;)
Thank ya, I take pride in my PHP Tongue
Reply
#7
Both methods work, nice tutorial.
Reply
#8
Thanks for the great tutorial, worked well.
Reply
#9
yeah both methods work
Reply
#10
Very nice TUT, this will help out alot of members
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  vb6.0 + mysql Anurag.91 1 1,791 09-08-2012, 04:19 PM
Last Post: spesificrelax
  Creating and inserting data into a PHP and MySQL Database Peter L 9 4,642 03-24-2012, 10:49 AM
Last Post: Haxalot
  VB.NET MySql , Help please booterphhp 2 1,875 03-19-2012, 11:13 AM
Last Post: RainbowDashFTW
  MySQL (Innodb vs MyISAM) Omniscient 15 10,748 07-22-2011, 03:15 PM
Last Post: developer99
  MySQL question ImFocuzz 8 3,144 07-17-2011, 06:57 PM
Last Post: Pedo bear

Forum Jump:


Users browsing this thread: 4 Guest(s)