PHP MySql数据库访问
PHP MySql數(shù)據(jù)庫訪問
計應(yīng)134?? 凌豪
1.MySql數(shù)據(jù)庫的連接
要操作MySql數(shù)據(jù)庫,首先必須與MySQl數(shù)據(jù)庫建立連接,連接MySQL服務(wù)器的語句如下:
<?php
$link = mysql_connect("localhost", "root", "root") or die("不能連接到數(shù)據(jù)庫服務(wù)器!
可能是數(shù)據(jù)庫服務(wù)器沒有啟動,或者用戶名密碼有誤!".mysql_error());?? //連接Mysql服務(wù)器
if($link){
echo "數(shù)據(jù)源連接成功!";
}
?>
2.選擇MySQL數(shù)據(jù)庫
在連接到MySQl數(shù)據(jù)庫服務(wù)器之后,使用mysql_select_db()函數(shù)來選擇數(shù)據(jù)庫,其實例代碼如下:
<?php
$link = mysql_connect("localhost", "root", "root") or die("不能連接到數(shù)據(jù)庫服務(wù)器!可能是數(shù)據(jù)庫服務(wù)器沒有啟動,或者用戶名密碼有誤!".mysql_error());?? //連接Mysql服務(wù)器
$db_selected=mysql_select_db("db_database18",$link);
//$db_selected=mysql_query("use db_database18",$link);
if($db_selected){
echo "數(shù)據(jù)庫選擇成功!";
}
?>
3.使用mysql_query()函數(shù)執(zhí)行SQL語句
要對數(shù)據(jù)庫中的表進行操作,通常使用mysql_query()函數(shù)執(zhí)行SQL語句,其基本語法格式如下:
mysql_query(string query[,resource link_indentifier])
4.向數(shù)據(jù)庫發(fā)送查詢
<?php
??? // 實例化mysqli類
??? $mysqliConn = new mysqli();
??? // 連接服務(wù)器,并選擇一個數(shù)據(jù)庫
??? // 錯誤的密碼
??? $mysqliConn->connect('127.0.0.1', 'root', 'root', 'db_test');
??? if ($mysqliConn->connect_error)
??? {
??????? printf("Unable to connect to the database:%s", $mysqliConn->connect_error);
??????? exit();
??? }
??? // 與數(shù)據(jù)庫交互
??? $query = 'select firstname, lastname, email from tb_test;';
??? // 發(fā)送查詢給MySQL
??? $result = $mysqliConn->query($query);
??? // 迭代處理結(jié)果集
??? while (list($firstname, $lastname, $email) = $result->fetch_row())
??? {
??????? printf("%s %s's email:%s<br/>", $firstname, $lastname, $email);
??? }
?? // 關(guān)閉連接
??? $mysqliConn->close();
?>
5.插入、更新和刪除數(shù)據(jù)
插入、更新和刪除使用的是insert、update和delete查詢完成的,其做法實際上與select查詢相同。示例代碼如下:
<?php
??? // 實例化mysqli類
??? $mysqliConn = new mysqli();
??? // 連接服務(wù)器,并選擇一個數(shù)據(jù)庫
??? // 錯誤的密碼
??? $mysqliConn->connect('127.0.0.1', 'root', 'root', 'db_test');
??? if ($mysqliConn->connect_error)
??? {
??????? printf("Unable to connect to the database:%s", $mysqliConn->connect_error);
??????? exit();
??? }
??? // 與數(shù)據(jù)庫交互
??? $query = 'select firstname, lastname, email from tb_test;';
??? // 發(fā)送查詢給MySQL
??? $result = $mysqliConn->query($query);
??? // 迭代處理結(jié)果集
??? while (list($firstname, $lastname, $email) = $result->fetch_row())
??? {
??????? printf("%s %s's email:%s<br/>", $firstname, $lastname, $email);
??? }
??? $query = "delete from tb_test where firstname = 'Yuan';";
??? $result = $mysqliConn->query($query);
??? // 告訴用戶影響了多少行
??? printf("%d row(s) have been deleted.<br/>", $mysqliConn->affected_rows);
??? // 重新查詢結(jié)果集
??? $query = 'select firstname, lastname, email from tb_test;';
??? // 發(fā)送查詢給MySQL
??? $result = $mysqliConn->query($query);
??? // 迭代處理結(jié)果集
??? while (list($firstname, $lastname, $email) = $result->fetch_row())
??? {
??????? printf("%s %s's email:%s<br/>", $firstname, $lastname, $email);
??? }
??? // 關(guān)閉連接
??? $mysqliConn->close();
?>
5.MySQL分頁顯示公告
?? 有時候在查詢一些數(shù)據(jù)時,為了更加方便的瀏覽查詢出的信息內(nèi)容,最好的方法就是通過分頁來顯示公告信息的內(nèi)容
下面主要是使用select語句動態(tài)檢索數(shù)據(jù)庫中的公告信息,并通過分頁技術(shù)完成對數(shù)據(jù)庫中公告信息的分頁輸出,主要部分代碼如下:
<?php
?? ??? $conn=mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫服務(wù)器連接錯誤".mysql_error());
?? ??? mysql_select_db("db_database18",$conn) or die("數(shù)據(jù)庫訪問錯誤".mysql_error());
?? ??? mysql_query("set names gb2312");
?? ???? /*? $_GET[page]為當(dāng)前頁,如果$_GET[page]為空,則初始化為1? */
?? ???? if ($_GET[page]==""){
?? ??? ? $_GET[page]=1;}
?? ???? if (is_numeric($_GET[page])){
?? ???? $page_size=4;??? ??? ??? ??? ??? ??? ??? ??? ??? ?//每頁顯示4條記錄
?? ???? $query="select count(*) as total from tb_affiche? order by id desc";? ?
?? ???? $result=mysql_query($query);???? ??? ??? ??? ??? ??? ?//查詢符合條件的記錄總條數(shù)
?? ???? $message_count=mysql_result($result,0,"total");?? ??? ?//要顯示的總記錄數(shù)
?? ???? $page_count=ceil($message_count/$page_size);?? ? ??? ?//根據(jù)記錄總數(shù)除以每頁顯示的記錄數(shù)求出所分的頁數(shù)
?? ???? $offset=($_GET[page]-1)*$page_size;?? ??? ??? ??? ??? ??? ?//計算下一頁從第幾條數(shù)據(jù)開始循環(huán) ?
?? ???? $sql=mysql_query("select * from tb_affiche order by id desc limit $offset, $page_size");
?? ???? $row=mysql_fetch_object($sql);
?? ??? ?? if(!$row){
?? ??? ??? ??? echo "<font color='red'>暫無公告信息!</font>";
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ?? do{
?? ??? ???? ?>
?? ??? ??? ??? ??? ?<?php
?? ??? ??? ??? ??? ??? ?}while($row=mysql_fetch_object($sql));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??>
???????????????????? </table>
????????????????????? <br>
????????????????????? <table width="550" border="0" cellspacing="0" cellpadding="0">
??????????????????????? <tr>
????????????????????????? <!--? 翻頁條 -->
? ??? ??? ??? ??? ??? ???? <td width="37%"> 頁次:<?php echo $_GET[page];?>/<?php echo $page_count;?>頁 記錄:
??????????????????? <?php echo $message_count;?> 條 </td>
?? ??? ??? ??? ??? ??? ??? ?<td width="63%" align="right">
?? ??? ??? ??? ??? ??? ??? ?<?php
?? ??? ??? ??? ??? ??? ??? ?/*? 如果當(dāng)前頁不是首頁? */
?? ??? ??? ??? ??? ??? ??? ?if($_GET[page]!=1){
?? ??? ??? ??? ??? ??? ??? ?/*? 顯示“首頁”超鏈接? */
?? ??? ??? ??? ??? ??? ??? ?echo? "<a href=page_affiche.php?page=1>首頁</a> ";
?? ??? ??? ??? ??? ??? ??? ?/*? 顯示“上一頁”超鏈接? */
?? ??? ??? ??? ??? ??? ??? ?echo "<a href=page_affiche.php?page=".($_GET[page]-1).">上一頁</a> ";
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?/*? 如果當(dāng)前頁不是尾頁? */
?? ??? ??? ??? ??? ??? ??? ?if($_GET[page]<$page_count){
?? ??? ??? ??? ??? ??? ??? ?/*? 顯示“下一頁”超鏈接? */
?? ??? ??? ??? ??? ??? ??? ?echo "<a href=page_affiche.php?page=".($_GET[page]+1).">下一頁</a> ";
?? ??? ??? ??? ??? ??? ??? ?/*? 顯示“尾頁”超鏈接? */
?? ??? ??? ??? ??? ??? ??? ?echo? "<a href=page_affiche.php?page=".$page_count.">尾頁</a>";
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?mysql_free_result($sql);
?? ??? ??? ??? ??? ??? ??? ?mysql_close($conn);
?? ??? ??? ??? ??? ??? ??? ??>
轉(zhuǎn)載于:https://www.cnblogs.com/linghao713/p/4911400.html
總結(jié)
以上是生活随笔為你收集整理的PHP MySql数据库访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业日志分析之linux系统messag
- 下一篇: php闭包函数简析