PHP使用mysqli连接MariaDB

简单的记录一下今天学到的 php使用mysqli连接Mysql数据库

一、mysql与mysqli的区别(摘抄自:https://www.cnblogs.com/gengyi/p/6544407.html)

1.什么是mysqli

PHP-MySQL 函数库是 PHP 操作 MySQL 资料库最原始的扩展库,PHP-MySQLi 的 i 代表 Improvement ,相当于前者的增强版,也包含了相对进阶的功能,另外本身也增加了安全性,比如可以大幅度减少 SQL 注入等问题的发生。

2. mysql与mysqli的概念相关

(1)mysql与mysqli都是php方面的函数集,与mysql数据库关联不大。

(2)在php5版本之前,一般是用php的mysql函数去驱动mysql数据库的,比如mysql_query()的函数,属于面向过程

(3)在php5版本以后,增加了mysqli的函数功能,某种意义上讲,它是mysql系统函数的增强版,更稳定更高效更安全,与mysql_query()对应的有mysqli_query(),属于面向对象,用对象的方式操作驱动mysql数据库。

3. mysql与mysqli的主要区别

(1)首先两个函数都是用来处理DB 的。

(2)mysqli 连接是永久连接,而mysql是非永久连接。什么意思呢? mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以很大程度的减轻服务器端压力

(3)mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。应用比较多的地方是 mysqli的事务。具体查看  http://cn.php.net/mysqli

二、实例:

数据表结构:

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>&nbsp;</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>                     

好了就这么多了,,加油加油加油加油加油加油加油加油

猜你喜欢

转载自www.cnblogs.com/lzwangshubo/p/10044499.html