PHP操作管理sql数据库

2020年10月30日 281点热度 3人点赞 0条评论

创建一个数据库操作类的PHP文件,文件内容:

<?php
//MySQL、MySQLi、SQLite 三合一数据库操作类
if(!defined('IN_CRONLITE'))exit();

$nomysqli=false;

if(defined('SQLITE')==true){
	class DB {
		var $link = null;

		function __construct($db_file){
			global $siteurl;
		$this->link = new PDO('sqlite:'.ROOT.'includes/sqlite/'.$db_file.'.db');
		if (!$this->link) die('Connection Sqlite failed.\n');
		return true;
        }

		function fetch($q){
			return $q->fetch();
		}
		function get_row($q){
			$sth = $this->link->query($q);
			return $sth->fetch();
		}
		function count($q){
			$sth = $this->link->query($q);
			return $sth->fetchColumn();
		}
		function query($q){
			return $this->result=$this->link->query($q);
		}
		function affected(){
			return $this->result->rowCount();
		}
		function error(){
			$error = $this->link->errorInfo();
			return '['.$error[1].'] '.$error[2];
		}
	}
}
elseif(extension_loaded('mysqli') && $nomysqli==false) {
    class DB {
        var $link = null;

        function __construct($db_host,$db_user,$db_pass,$db_name,$db_port){
            
            $this->link = mysqli_connect($db_host, $db_user, $db_pass, $db_name, $db_port);
            
            if (!$this->link) die('Connect Error (' . mysqli_connect_errno() . ') '.mysqli_connect_error());
            
            //mysqli_select_db($this->link, $db_name) or die(mysqli_error($this->link));
            
 
mysqli_query($this->link,"set sql_mode = ''");
 //字符转换,读库
mysqli_query($this->link,"set character set 'utf8'");
//写库
mysqli_query($this->link,"set names 'utf8'");
 
	return true;
	}
		function fetch($q){
			return mysqli_fetch_assoc($q);
		}
		function get_row($q){
			$result = mysqli_query($this->link,$q);
			return mysqli_fetch_assoc($result);
		}
		function count($q){
			$result = mysqli_query($this->link,$q);
			$count = mysqli_fetch_array($result);
			return $count[0];
		}
		function query($q){
			return mysqli_query($this->link,$q);
		}
		function escape($str){
			return mysqli_real_escape_string($this->link,$str);
		}
		function insert($q){
			if(mysqli_query($this->link,$q))
				return mysqli_insert_id($this->link); 
			return false;
		}
		function affected(){
			return mysqli_affected_rows($this->link);
		}
		function insert_array($table,$array){
			$q = "INSERT INTO `$table`";
			$q .=" (`".implode("`,`",array_keys($array))."`) ";
			$q .=" VALUES ('".implode("','",array_values($array))."') ";
			
			if(mysqli_query($this->link,$q))
				return mysqli_insert_id($this->link);
			return false;
		}
		function error(){
			$error = mysqli_error($this->link);
			$errno = mysqli_errno($this->link);
			return '['.$errno.'] '.$error;
		}
		function close(){
			$q = mysqli_close($this->link);
			return $q;
		}
	}
} else { // we use the old mysql
	class DB {
		var $link = null;

		function __construct($db_host,$db_user,$db_pass,$db_name,$db_port){

		$this->link = @mysql_connect($db_host.':'.$db_port, $db_user, $db_pass);
            
		if (!$this->link) die('Connect Error (' . mysql_errno() . ') '.mysql_error());
            
			mysql_select_db($db_name, $this->link) or die(mysql_error($this->link));

mysql_query("set sql_mode = ''");
//字符转换,读库
mysql_query("set character set 'utf8'");
//写库
mysql_query("set names 'utf8'");
 

	return true;
		}
		function fetch($q){
			return mysql_fetch_assoc($q);
		}
		function get_row($q){
			$result = mysql_query($q, $this->link);
			return mysql_fetch_assoc($result);
		}
		function count($q){
			$result = mysql_query($q, $this->link);
			$count = mysql_fetch_array($result);
			return $count[0];
		}
        function query($q){
			return mysql_query($q, $this->link);
		}
		function escape($str){
			return mysql_real_escape_string($str, $this->link);
		}
		function affected(){
			return mysql_affected_rows($this->link);
		}
		function insert($q){
			if(mysql_query($q, $this->link))
				return mysql_insert_id($this->link);
			return false;
		}
		function insert_array($table,$array){
			$q = "INSERT INTO `$table`";
			$q .=" (`".implode("`,`",array_keys($array))."`) ";
			$q .=" VALUES ('".implode("','",array_values($array))."') ";

			if(mysql_query($q, $this->link))
				return mysql_insert_id($this->link);
			return false;
		}
		function error(){
			$error = mysql_error($this->link);
			$errno = mysql_errno($this->link);
			return '['.$errno.'] '.$error;
		}
		function close(){
			$q = mysql_close($this->link);
			return $q;
		}
	}

}
?>

导入后即可执行以下操作:

<?php
/*传入数据库操作类PHP文件*/
define('IN_CRONLITE', true);
define('SYSTEM_ROOT', dirname(__FILE__).'/');
include_once(SYSTEM_ROOT."mysql.php");

/*数据库配置*/
$dbconfig=array(
	'host' => 'localhost', //数据库服务器
	'port' => '3306', //数据库端口
	'user' => '', //数据库用户名
	'pwd' => '', //数据库密码
	'dbname' => '', //数据库名
);
/*
*使用示例
*
*if($DB -> query("sql语句")){
*    echo '成功!';
*}else{
*    echo '失败!';
*}
*/

/*查询数据库*/
$rs=$DB->query("select * from 表 where id = ".$_POST['id']." limit 1");
$conf = $DB->fetch($rs);
echo $conf['表单'];

/*删除数据库*/
if($DB->query("DELETE FROM `表` WHERE `apis`.`id` = ".$id)){
 echo '删除成功!';
 }else{
 echo '删除失败!';
}

/*清空表内数据*/
if($DB->query("TRUNCATE TABLE `表`")==true){
    echo '清空成功';
    }else{
    echo'清空失败';
    }

/*添加表数据*/
 $sql = $DB->query("INSERT INTO `表` (`id`) VALUES ($id)");
if($sql){
    echo '添加成功';
    }else{
    echo '添加失败';
    }

/*修改表数据*/
if($DB -> query("UPDATE `表` SET `索引` = '修改内容' WHERE `表`.`主键` = ".$id) == true){
    echo '修改成功!!!';
}else{
    echo '修改失败!!!';
}
?>

2D天天

开开心心听歌就好。

文章评论

您需要 登录 之后才可以评论