不用担心。在这里,我发布了一些用于 DB 操作的自定义类。
Check this.
**Config.php**
<?php
if(!isset($_SERVER['DOCUMENT_ROOT'])){
if(isset($_SERVER['SCRIPT_FILENAME'])){
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',
substr($_SERVER['SCRIPT_FILENAME'], 0,
0-strlen($_SERVER['PHP_SELF']))); }; };
if(!isset($_SERVER['DOCUMENT_ROOT'])){
if(isset($_SERVER['PATH_TRANSLATED'])){
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',
substr(str_replace('\\\\', '\\',
$_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF'])));
}; };
# Define DB local
define('DBHOST','localhost'); // host name//192.168.1.20
define('DBUSER','xxxxxx'); // mysql user name
define('DBPASS','xxxxxxx'); // mysql pasword//142536
define('DBNAME','xxxxx'); // databse name
define('SITE_NAME','www.xxxxxx.co.in');
?>
Save this as config.php
Next For DB operations Like Insert, Update, Select, Delete
**db_class.inc**
> <?php
class db_mysql
{
var $database;
var $hostname;
var $username;
var $password;
var $link;
function db_mysql($hostname, $username, $password, $database)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
function connect()
{
if(!$this->link = @mysql_connect($this->hostname,$this->username,$this->password) )
{
echo "Cannot Connect to host: ".$this->hostname;
exit;
return false;
}
else
{
return true;
}
}
function select_db()
{
if(!@mysql_select_db($this->database, $this->link) )
{
echo "Cannot select database: ".$this->database;
exit;
return false;
}
else
{
return true;
}
}
function query($query)
{
$result = @mysql_query($query, $this->link) or die($query.'--<font color=red>'.mysql_error().'</font>');
return $result;
}
function fetch_one_row($query)
{
$result = @mysql_query($query, $this->link) or die($query.'--<font color=red>'.mysql_error().'</font>');
$array = @mysql_fetch_row($result);
if(is_array($array)) {
foreach($array as $key => $val)
$fetch_array[$key] = stripslashes($val);
}
return $fetch_array;
}
function fetch_array($result)
{
$array = @mysql_fetch_array($result);
if(is_array($array)) {
foreach($array as $key => $val)
$fetch_array[$key] = stripslashes($val);
}
return $fetch_array;
}
function fetch_row($result)
{
$array = @mysql_fetch_row($result);
if(is_array($array)) {
foreach($array as $key => $val)
$fetch_array[$key] = stripslashes($val);
}
return $fetch_array;
}
function fetch_assoc($result)
{
$array = mysql_fetch_assoc($result);
if(is_array($array)) {
foreach($array as $key => $val)
$fetch_array[$key] = stripslashes($val);
}
return $fetch_array;
}
function delete_id($id, $field, $table)
{
if( is_array($id) )
{
foreach( $id AS $i )
{
$this->query("DELETE FROM $table WHERE $field = '$i'");
}
}
else
{
$this->query("DELETE FROM $table WHERE $field = '$id'");
}
}
function delete_sel_value($table,$field,$value)
{
$result=$this->query("SELECT $field FROM $table WHERE $field='$value'");
$count = $this->fetch_row($result);
return $count;
}
function num_rows($result)
{
return @mysql_num_rows($result);
}
function affected_rows()
{
return @mysql_affected_rows();
}
function insert_id()
{
return @mysql_insert_id();
}
function value_exists($array, $table)
{
/*array contains values of the field name and field value*/
foreach($array as $key => $value)
{
$condition .= $key." = '".$value."' and ";
}
$condition = substr($condition,0,-5);
$result = $this->query("SELECT COUNT(*) as cnt FROM ".$table." WHERE ".$condition);
$count = $this->fetch_row($result);
return $count[0];
}
function get_count($table,$condition='')
{
if($condition) $condition = " WHERE " . $condition;
$result = $this->query("SELECT COUNT(*) as cnt FROM ".$table . $condition);
$count = $this->fetch_row($result);
return $count[0];
}
function update_from_array($array, $table, $field, $id)
{
$query = "UPDATE $table SET ";
while(@list($key,$value) = @each($array))
{
if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false))
{
$fields[] = "$key='$value'";
}
else
{
$fields[] = "$key=$value";
}
}
$query .= implode(', ', $fields);
$query .= " WHERE ".$field." = '$id'";
$this->query($query);
return true;
}
function insert_from_array($array, $table)
{
while( @list($key,$value) = @each($array) )
{
$field_names[] = "$key";
if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false) )
$field_values[] = "'$value'";
else
$field_values[] = "$value";
}
$query = "INSERT INTO $table (";
$query .= implode(', ', $field_names);
$query .= ') VALUES (' . implode(',', $field_values) . ')';
$this->query($query);
return true;
}
}
// END CLASS
?>
//Read by GET method, if URL like WWW.x.com/index.php?Get_par=req_Operation&count=1
//Use can use post method for getting **form** values
> if(function_exists($_GET['Get_par'])) {
>
> $_GET['Get_par']();
>
>
> }
//if get parameter value becomes **req_Operation**
> function req_Operation() {
> require('config.php');
> require('db_class.inc.php');
> $db = new db_mysql(DBHOST,DBUSER,DBPASS,DBNAME);
> $db->connect();
> $db->select_db();
>
> $var1 =$_POST['encrypt1'];
>
> $var2 =$_POST['encrypt2'];
>
>
> $query = "SELECT X FROM table_details where
> trim(table_key)='$var1'";
>
> $result = mysql_query($query) or die("Couldn't execute query.because:
>
> ".mysql_error());
>
>
> }
>
> ?>