//为了使用方便,我进行了函数封装,
//这对于之后数据库的存储过程的学习有很大帮助
//数据库连接
function dblnit($host="localhost",$user="root",$pwd="root",$dbname,$charset="utf8"){mysql_connect($host,$user,$pwd)or die("数据库连接失败");
mysql_select_db($dbname);
mysql_query("set name($charset)");
}
//数据表建立
function jbiao($bname){
$sql = "CREATE TABLE $bname (
JID int not null,//不为空
ID int not null auto_increment,//不为空,自增
KEY(ID),
UserName varchar(25) PRIMARY KEY,//主键
Phones varchar(25),
Email varchar(25),
PassWord varchar(25)
)";
mysql_query($sql);
}
//增,(插入数据1)
function chashu($bname,$age){
$age=intval($age); //执行类型转换,防止SQL注入 ,字符串加引号,
$sql="INSERT INTO $bname (UserName, PassWord, XingBie, Phones ,Age)
VALUES
('$_POST[username]','$_POST[password]','$_POST[xingbie]','$_POST[phones]',$age)";
//VALUES可插入多个(数据)
mysql_query($sql);
}
//增,(插入数据2)
function chashuj($bname,$age){
$age=intval($age); //执行类型转换,防止SQL注入
$sql="INSERT INTO $bname username=,password=,age=,xingbie=,phone=";
mysql_query($sql);
}
//删
function delet($bname,$where){
$sql="delete from ($bname) where ($where)";
mysql_query($sql);
return mysql_affected_row();//得到收到影响的行数
//DELETE FROM $bname WHERE name = 'mike'
//DELETE FROM $bname WHERE name = 'mike' LIMIT 6;
//条件删除6行
//DELETE FROM $bname WHERE name = 'mike' ORDER BY id DESC LIMIT 6;
//条件排序删除6行
}
//改
function gaba($bname,$age,$id){
$sql="update $bname set age=$age where id=$id";
mysql_query($sql);
}
//查
function chaxu($bname,$id){
$sql="select * from $bname where $id";
// $result=mysql_query("SELECT * FROM $bname ORDER BY $id DESC");
// 查询数据表中的所有数据,并按照id降序排列
//$sql="select * from $bname where $id like 'z%'";
//以z在开头的(%)任意长度字符
//$sql="select * from $bname where $id like '_z%'";
//第二个字符是z的任意长度
//$sql="select * from $bname where $id like '%z%'";
//表示有z字符的
$result=mysql_query($sql);
mysql_num_rows($result); //获取数据表的数据条数
$result_arr=mysql_fetch_assoc($result); //数组显示
return mysql_affected_row();//得到收到影响的行数
}
/*--------------------------
//首先进行非空排错
if(!isset($_POST['name'])){
die('name is not define');
}
if(!isset($_POST['age'])){
die('age is not define');
}
$name=$_POST['name'];
$age=$_POST['age'];
if(empty($name)){
die('name is empty');
}
if(empty($age)){
die('age is empty');
}
--------------------*/
/*---------------------
//排错并返回
if(mysql_error()){
echo mysql_error();
}else{
header("Location:allusers.php");
}
---------------------*/
/*-----------------
<?php
define('MYSQL_HOST','localhost');
define('MYSQL_USER','root');
define('MYSQL_PW','');
//创建一个常量
require_once 'config.php';
//调用其他php函数