简单的记录一下今天学到的 php使用mysqli链接Mysql数据库php
PHP-MySQL 函数库是 PHP 操做 MySQL 资料库最原始的扩展库,PHP-MySQLi 的 i 表明 Improvement ,至关于前者的加强版,也包含了相对进阶的功能,另外自己也增长了安全性,好比能够大幅度减小 SQL 注入等问题的发生。html
(1)mysql与mysqli都是php方面的函数集,与mysql数据库关联不大。mysql
(2)在php5版本以前,通常是用php的mysql函数去驱动mysql数据库的,好比mysql_query()的函数,属于面向过程sql
(3)在php5版本之后,增长了mysqli的函数功能,某种意义上讲,它是mysql系统函数的加强版,更稳定更高效更安全,与mysql_query()对应的有mysqli_query(),属于面向对象,用对象的方式操做驱动mysql数据库。数据库
(1)首先两个函数都是用来处理DB 的。数组
(2)mysqli 链接是永久链接,而mysql是非永久链接。什么意思呢? mysql链接每当第二次使用的时候,都会从新打开一个新的进程,而mysqli则只使用同一个进程,这样能够很大程度的减轻服务器端压力。安全
(3)mysqli封装了诸如事务等一些高级操做,同时封装了DB操做过程当中的不少可用的方法。应用比较多的地方是 mysqli的事务。具体查看 http://cn.php.net/mysqli服务器
2、实例:函数
数据表结构:post
1.DBcon.php:
<?php $cn=new mysqli("hostname","username","password","databasename",port);//获取数据库链接 if (!$cn)//判断链接是否为空 { die("链接错误: " . mysqli_connect_error());//链接失败 打印错误日志 } $cn->query("SET NAMES utf8");//设置 字符集为utf8格式 $cn->select_db("qquser");//选择要操做的数据表
2.query.php
<?php require ("DBcon.php"); // 相对路径引用数据库链接类 $sql="select * from qquser"; mysqli_query($cn,$sql); //传入数据库链接参数,sql字符串。 $res=$cn->query($sql); //接收查询产生的结果集 $row=mysqli_fetch_assoc($res); //将结果集赋值给数组对象 echo $row["userid"]."".$row["username"]."".$row["userpassword"]; //输出结果
3.带有html语句的例子
<?php #链接数据库 $conn = mysqli_connect("www.jnvc.club","root","w@ng123","qq",3306); #判断是否链接成功 if(!$conn){ echo "失败"; } //选择数据库 mysqli_select_db($conn,"qq"); //准备sql语句 $sql = "select * from qquser"; //发送sql语句 $obj = mysqli_query($conn,$sql); echo "<center>"; echo "<table border = 1 cellspacing = '0' cellpadding = '10'>"; echo "<th>编号</th><th>姓名</th><th>密码</th>"; while($row = mysqli_fetch_assoc($obj)){ echo "<tr>"; echo '<td>'.$row['userid'].'</td>'; echo '<td>'.$row['username'].'</td>'; echo '<td>'.$row['userpassword'].'</td>'; echo "</tr>"; } echo "</table>"; echo "<center>"; //关闭链接 mysqli_close($conn);
4.使用mysql方法链接mysql数据库的网页留言板
select.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <?php require("shujuku.php"); $result=mysql_query("select * from user",$conn); ?> <a href="addform.php">添加记录</a> <table border="1" width="95%"> <tr bgcolor="#e0e0e0"> <th>ID</th> <th>标题</th> <th>内容</th> <th>留言者</th> <th>Email</th> <th>添加记录</th> </tr> <?php while($row=mysql_fetch_assoc($result)){ ?> <tr> <td><?php echo $row["id"]?></td> <td><?php echo $row["title"]?></td> <td><?php echo $row["cotent"]?></td> <td><?php echo $row["author"]?></td> <td><?php echo $row["email"]?></td> <td><a href="delete.php?id=5" onclick="return confirm('确认要删除吗?')">删除</a></td> </tr> <?php }?> </table> <body> </body> </html>
shujuku.php : <?php $conn=mysql_connect("118.24.10.164","root","w@ng123"); mysql_query("set names 'utf-8'"); mysql_select_db("php",$conn); ?>
insert.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php date_default_timezone_set('PRC'); require("shujuku.php"); $title=$_POST['title']; $cotent=$_POST['content']; $author=$_POST['author']; $email=$_POST['telephone']; $IP=$_SERVER['REMOTE_ADDR']; $time=date('Y-m-d h:i:s'); if(isset($_POST['submit'])){ mysql_query("insert into user(title,cotent,author,email,ip,addtime,sex) values('$title','$cotent','$author','$email','$IP','$time','女')")or die("哦哦"); } ?> </body> </html>
addform.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h2 align="center" >请您填写留言</h2> <form method="post" action="insert.php"> <table border="1" align="center" width="400" cellpadding="2"> <tr> <td width="125">留言标题:</td> <td width="275"> <input type="text" name="title"></td> </tr> <tr> <td width="125">留言者:</td> <td width="275"> <input type="text" name="author"></td> </tr> <tr> <td >联系方式:</td> <td> <input type="text" name="content"></td> </tr> <tr> <td width="125">留言内容:</td> <td width="275"> <textarea cols="30" rows="5" name="content"></textarea></td> </tr> <tr> <td> </td> <td width="275"> <input type="submit" name="submit" value="提交"></td> </tr> </table> </form> </body> </html>
delete.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php require("shujuku.php"); $id=$_GET['id']; echo $id; mysql_query("delete from user where id=$id")or die("哦哦"); ?> </body> </html>
好了就这么多了,,加油加油加油加油加油加油加油加油