原生sql 封装

<?php

return array (
    'default' => array (
        'hostname' => '127.0.0.1',
        'database' => 'csshop',
        'username' => 'root',
        'password' => 'root',
        'tablepre' => 'bz_',
        'charset' => 'utf8',
        'type' => 'mysql',
        'debug' => true,
        'pconnect' => 0,
        'autoconnect' => 0
        ),
);
文件  database.php
**************************************************************
$mysql_user=include('../database.php');//得到数据库配置
        $username=$mysql_user['default']['username'];
        $password=$mysql_user['default']['password'];
        $tablepre=$mysql_user['default']['tablepre'];
        $database=$mysql_user['default']['database'];

        $con = mysqli_connect($mysql_user['default']['hostname'],$username,$password)or die("连接服务器失败");//句柄

mysqli_select_db($con,$database) or die("数据库连接失败");//选择数据库 

<?php
/**
	 * $con 连接数据库对象
	 * $table  表名
	 * $where 条件
	 * $field 查询内容
	 * $order 排序规则
	 * $all 是否只查一条 
	 * $tablepre 表前缀
	 */
	function select ($con, $table, $where , $field, $order = ' id ASC ' ,$all = '' ,$tablepre = '') {
        if( $all ){
            $sql = ' SELECT ' .$field. ' FROM '.$tablepre.''.$table.' where ' .$where .' order by "' .$order. '"';
        }else{
            $sql = ' SELECT ' .$field. ' FROM '.$tablepre.''.$table.' where ' .$where. ' limit 1';
        }
        $result2= mysqli_query ( $con, $sql);
        if (!$result2) {
            printf("Error: %s\n", mysqli_error ( $con));
            exit ();
        }
        if( $all ){
        	$row = $result2->num_rows;
        	while ($row> 0) {
	        	$data[] = mysqli_fetch_array ($result2);
	        	$row -=1;
	        }
        }else{
        	$data = mysqli_fetch_array ($result2);
        }
        return $data;
    }
    function closemysql($con){
    	mysqli_close($con);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值