search
Homephp教程php手册php---数据库类封装

为了节省以后的时间,今天封装了操作sql语句的一个类,在此保存起来,方面以后使用。

这个类的文件名:SqlTool.class.php

主要有dql和dml两个函数

看下面的源码“

<?php

   class SqlTool{
      private $conn;
      private $username="root";
      private $password="1234";
      private $host="127.0.0.1";
      private $db="test";
     
     function SqlTool(){
        $this->conn=mysql_connect($this->host,$this->username,$this->password);
        if(!$this->conn)
        {
            die  ("连接失败".mysql_error($this.conn));
        }
        mysql_select_db($this->db,$this->conn);
        mysql_query("set names utf8");
     }
     
     //完成select
     function execute_dql($sql)
     {
        $result = mysql_query($sql,$this->conn);
        $arrTemp = array();
        $counter = 0;
        while($row = mysql_fetch_assoc($result))
        {
         $arrTemp[$counter] = $row;
          $counter++;
        }
        mysql_free_result($result);
        mysql_close($this->conn);
        
        return json_encode($arrTemp);
     }
     
     //完成dml
     public  function execute_dml($sql)
     {
        $res=mysql_query($sql,$this->conn);
        if(!$res)
        {
            return 0; //执行失败
        } 
        else
        {
            if(mysql_affected_rows($this->conn)>0)
            { 
                return 1;  //执行成功
            } 
            else
            {
                return 2; //没有行数收到影响
            }
        }
        
     }
   } 

?>

演示一下使用方法:

<?php
 
  @header("content-Type:text/html;charset:utf-8");
 
  require_once "SqlTool.class.php";
  $sqlStr = "insert into student(number,name,age) values('200911230','huzhuxi','45');";
  $sqlTool = new SqlTool();
  $result1=$sqlTool->execute_dml($sqlStr);
  echo "结果为:".$result1."<br/>";
  
  $sqlStr1 = "select * from student";
  $resultStr = $sqlTool->execute_dql($sqlStr1);
  echo "当前数据:<br/>".$resultStr;
?>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment