自己写的数据库调用

 <?php

/**
title:数据库操作

*/
$host="127.0.0.1";
$usert="root";
$passu="jinjin";
$npp="test";

$conn=mysql_connect($host,$usert,$passu) or die("连接数据库失败");
mysql_select_db($npp);
//单项批量查找
 function lookr($a,$b) { 
  $sql=mysql_query("SELECT * FROM ".$a);
	while($ut = mysql_fetch_array( $sql)){
    
	echo $ut[$b]."<br>";
	}
 }
 //条件批量模糊查找
 function lookrcl($a,$b,$c,$d) { 
  $sql=mysql_query("SELECT * FROM $a where $c like '%$d%'");
	while($ut = mysql_fetch_array($sql)){
    
	echo $ut[$b]."<br>";
	}
 }
 //精确查找
function lookrc($a,$b,$c,$d) { 
  $sql=mysql_query("SELECT * FROM $a where $c ='%$d%'");
	while($ut = mysql_fetch_array($sql)){
    
	echo $ut[$b]."<br>";
	}
 }
 //条件删除操作
 function del($a,$b,$c) { 
  $sql=mysql_query("DELETE FROM $a WHERE $b = '$c'");
	$result = mysql_query($sql);

if(mysql_affected_rows() > 0) {

echo "删除成功";

}else{
echo "删除失败";	
}

mysql_close();
 }
 //添加操作
 function add($a,$b,$c) { 
  if(mysql_query("INSERT INTO $a ($b) VALUES('$c')")){
	
    
	echo "添加成功";
	}else{
		echo "添加失败";
	}
 }
 //更新操作
 
 function up($a,$b,$c,$d,$e) { 
  if(mysql_query("UPDATE $a SET $b='$c' where $d='$e'")){
	
    
	echo "更新成功";
	}else{
		echo "更新失败";
	}
 }
 
?>
<h2>说明文档</h2>
include "sql.php"; //调用主文件<br>
lookr("lei","title");//单项批量查找<br>
lookrc("lei","id","","");//精确查找<br>
lookrcl("lei","id","","");//模糊查找<br>
del("","","");//删除<br>
add("","","");//添加操作<br>
up("lei","title","没有问题吗","id","5");//更新操作<br>

评论

刷新

友情链接