Post: Basic PHP database connect
07-19-2012, 10:16 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Again, a basic script, but you can use it for applications on a frame work:

    
<?php

class Database
{
var $con;
var $sel;
var $res;
var $assoc;
var $escape;

function connect()
{
$this->con = mysql_connect("localhost", "host", "") or die(mysql_error());
$this->sel = mysql_select_db("test") or die(mysql_error());
}

function close()
{
if (isset($this->connect)) {
mysql_close($this->connect());
unset($this->connect);
}
}

function query($sql)
{
$this->res = mysql_query($sql);
return $this;
}

function fetch($type)
{
switch ($type) {
case 1:
$this->assoc = mysql_fetch_assoc($this->res);
return $this;
break;
case 2:
$this->assoc = mysql_fetch_array($this->res);
return $this;
break;
case 3:
$this->assoc = mysql_fetch_row($this->res);
return $this;
break;
}
}

function escape($escape)
{
$this->escape = mysql_real_escape_string($escape);
echo $this->escape;
}

function get($col)
{
printf($this->assoc[$col]);
}

}
?>


You can use it like so:

    
<?php
include("applications/libs/database.php");
$database = new Database();
$database->connect();
$database->query("SELECT * FROM test WHERE name='nat'")->fetch(2)->get('pass'Winky Winky;
//You better escape this mother before running dude! Winky Winky
$database->escape("'1='");
?>


See? the above code is much easier than the normal way like:

    
$res = mysql_query("SELECT * FROM test");
$row = mysql_fetch_assoc($res);
echo $row['name'];

You can allow other apps to connect of like:

    
<?php

class MyExtension Extends Database
{

}

?>
(adsbygoogle = window.adsbygoogle || []).push({});

The following 2 users say thank you to RuffRage for this useful post:

Gnosis, LEzStarz
07-19-2012, 11:08 AM #2
Originally posted by Embryoo View Post
Again, a basic script, but you can use it for applications on a frame work:

    <?php

class Database
{
function __construct()
{
$this->connect();
$this->close();
$this->query();
}

public function connect()
{
$con = mysql_connect("","","") or die(mysql_error());
$sel = mysql_select_db("") or die(mysql_error());
}

public function close()
{
if(isset($this->connect))
{
mysql_close($this->connect());
unset($this->connect);
}
}

public function query($sql)
{
$res = mysql_query($res) or die(mysql_error());
}




}
$database = new Database;
$database->connect();
$database->query("SELECT * FROM users");
$database->close();



?>


You can allow other apps to connect of like:

    
<?php

class MyExtension Extends Database
{

}

?>


Good job man, I am learning OOP and PDO now, and it is a whole new level of PHP. So this script is really useful! Smile

The following 2 users say thank you to LEzStarz for this useful post:

Gnosis, RuffRage
07-24-2012, 12:06 PM #3
Updated the class, it's easier now and less lines of code when using it.

The following user thanked RuffRage for this useful post:

Gnosis
07-24-2012, 03:42 PM #4
xAssasin
☯ Y.O.L.O ☯
Wow Really good job men, its not simple :y:

The following 2 users say thank you to xAssasin for this useful post:

Gnosis, RuffRage
07-24-2012, 06:13 PM #5
Originally posted by xAssasin View Post
Wow Really good job men, its not simple :y:


Thanks man. I have updated it again :P

The following user thanked RuffRage for this useful post:

Gnosis

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo