<?php
class Db
{
/*connection parameters*/
var $host='';
var $user='';
var $password='';
var $database='';
var $persistent=false;
/*data base handle*/
var $conn=NULL;
/*query resoult*/
var $rsult=false;
//-------------------------------------------------------------------
function DB($host,$user,$password,$database,$persistent=false)
{
$this->host=$host;
$this->user=$user;
$this->password=$password;
$this->database=$database;
$this->persistent=$persistent;
}
//--------------------------------------------------------------------
function open()
{
/*chose the type of connection function*/
if ($this->persistent) {
$func='mysql_pconnect';
}else{
$func='mysql_connect';
}
$this->conn=$func($this->host,$this->user,$this->password);
if (!$this->conn){
return false;
}
if (@!mysql_select_db($this->database,$this->conn)){
return false;
}
return true;
}
//--------------------------------------------------------------------
function close()
{
return (@mysql_close($this->conn));
}
//--------------------------------------------------------------------
function error()
{
return(mysql_error());
}
//--------------------------------------------------------------------
function query($sql)
{
$this->result=@mysql_query($sql,$this->conn);
return($this->result!=false);
}
//--------------------------------------------------------------------
function affectedrows()
{
return(@mysql_affected_rows($this->conn));
}
//--------------------------------------------------------------------
function numrows()
{
return (@mysql_num_rows($this->result));
}
//--------------------------------------------------------------------
function fetchobject()
{
return (@mysql_fetch_object($this->result,MYSQL_ASSOC));
}
//--------------------------------------------------------------------
function fetcharray()
{
return(@mysql_fetch_array($this->result,MYSQL_NUM));
}
//--------------------------------------------------------------------
function fetchassoc()
{
return(@mysql_fetch_assoc($this->result));
}
//--------------------------------------------------------------------
function freeresult()
{
return(@mysql_free_result($this->result));
}
}
?>
سلام
قطعه کد بالا از کتاب ساحر برداشته شده و واسه من جواب داده شما این کدهارو داخل یک فایل با پسوند php ذخیره کنید و بعد هر جا خواستید اتصالی به دیتا بیس داشته باشید کافیه این فایل رو با تکه کد زیر فراخوانی و استفاده کنید.
require_once("filename.php");
$conname=new DB('localhost','user',pass'','bank name');