PHP使用mysqli鏈接MariaDB

簡單的記錄一下今天學到的 php使用mysqli鏈接Mysql數據庫php

1、mysql與mysqli的區別(摘抄自:https://www.cnblogs.com/gengyi/p/6544407.html)

1.什麼是mysqli

PHP-MySQL 函數庫是 PHP 操做 MySQL 資料庫最原始的擴展庫,PHP-MySQLi 的 i 表明 Improvement ,至關於前者的加強版,也包含了相對進階的功能,另外自己也增長了安全性,好比能夠大幅度減小 SQL 注入等問題的發生。html

2. mysql與mysqli的概念相關

(1)mysql與mysqli都是php方面的函數集,與mysql數據庫關聯不大。mysql

(2)在php5版本以前,通常是用php的mysql函數去驅動mysql數據庫的,好比mysql_query()的函數,屬於面向過程sql

(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服務器

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>&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>                     

好了就這麼多了,,加油加油加油加油加油加油加油加油

相關文章
相關標籤/搜索