Home  >  Article  >  Backend Development  >  A simple php mysql operation class

A simple php mysql operation class

WBOY
WBOYOriginal
2016-07-25 08:56:401187browse
This article shares a simple PHP and MySQL operation class. It is very simple and mainly focuses on data connection and query. Friends in need can refer to it.

Share a piece of php and mysql operation code for reference by beginners. A simple class uses php and mysql database connection class, code:

<?php 
/**
* php、mysql连接类
* by bbs.it-home.org
*/
//----
/*
include "database.php"; //数据库配置信息

$db_host="localhost"; 
$db_user="user"; 
$db_pass="user"; 

$username=$_POST["txtUserName"]; 
$password=$_POST["txtPassword"]; 

$dbobj=new db; 
$dbobj->db_connect($db_host, $db_user, $db_pass,$db_name); 
$query="insert db_user values('$username','$password')"; 
$result=$dbobj->db_updateData($query); 
*/ 

class db 
{ 

var $query; 
var $numrows; 
  function db_connect($db_host, $db_user, $db_pass,$db_name)  
  { 
  global $ln; 
  global $result; 

  $db_host1=$db_host; 
  $db_user1=$db_user; 
  $db_pass1=$db_pass; 
  $db_name1=$db_name; 

 $ln = mysql_connect($db_host1, $db_user1, $db_pass1) or die("cannot connect"); 
 mysql_select_db($db_name1,$ln); 
  } 

  function db_selectData($query) 
  { 
global $ln; 
  global $result; 

  $this->query=$query; 
  $result= mysql_query($this->query,$ln); 
  return $result; 
  
  } 

  function db_deleteData($query) 
  { 
  global $ln; 
  global $result; 

 $this->query=$query; 
 $result= mysql_query($this->query,$ln); 
 return $result; 
  } 

  function db_updateData($query) 
  { 
   global $ln; 
 global $result; 
   $this->query=$query; 
 $result= mysql_query($this->query,$ln); 
 return $result; 

  } 

  function db_countRows($res) 
  { 
  global $ln; 
  global $result; 
  $this->numrows = mysql_num_rows($result); 
  return $this->numrows; 
  } 

  function db_close() 
  { 
  global $ln; 
  mysql_close($ln); 
  } 
}
?>


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