1 //建立数据库连接,数据库地址127.0.0,用户名root,密码root 2 $dbConn = mysql_connect('127.0.0.1', 'root', 'root'); 3 mysql_query('set names utf8'); //指定数据字符集 4 mysql_select_db('test', $dbConn); //指定数据库test 5 6 //增 7 $insertSql = 'insert into users(uname, email) values("刘强东", "liuqiangdao@jd.com")'; 8 var_dump($insertSql); 9 $result = mysql_query($insertSql, $dbConn); //成功返回true,失败返回false 10 11 //删 12 $deleteSql = "delete from users where id=2"; 13 $result = mysql_query($deleteSql, $dbConn); 14 15 //改 16 $updateSql = 'update users set email="liuqiangdong@163.com" where uname="刘强东"'; 17 $result = mysql_query($updateSql, $dbConn); 18 19 //查 20 $strSql = 'select * from users'; 21 $result = mysql_query($strSql, $dbConn); 22 //$result数据类型不能直接显示,要使用mysql_fetch_array()一行一行的取出 23 while($row = mysql_fetch_array($result)){ 24 var_dump($row); 25 } 26 27 //关闭连接 28 mysql_close($dbConn);