1、數據庫函數:php
mysql_connect(數據庫地址,用戶名,密碼)mysql
mysql_select_db(數據庫名)sql
mysql_set_chartset(‘編碼’) //PHP5.2.3之後的函數數據庫
mysql_query(「set names 'gbk'」) 數組
函數:網絡
mysql_query(SQL語句或命令) 函數
mysql_fetch_array(資源數據) //返回下標與鍵名數組 post
mysql_fetch_row(資源數據) //返回下標數組 fetch
mysql_fetch_object(資源數據) //返回對象形式調用 編碼
mysql_num_rows(數據資源)
mysql_close(數據資源)
2、微博客實現:
一、首頁index.php
<a href='add.php'>添加內容</a> <hr> <hr> <?php include 'conn.php'; // 引入鏈接數據庫 $sql = "SELECT * FROM `news` order by id desc limit 10"; $query = mysql_query ( $sql ); while ( $rs = mysql_fetch_array ( $query ) ) { ?> <h2>標題:<?php echo $rs['title'] ?> | <a href=''>編輯</a> | <a href='del.php?del=<?php echo $rs['id'] ?>'>刪除</a> | </h2> <li>時間:<?php echo $rs['dates'] ?></li> <p><?php echo $rs['contents'] ?></p> <hr> <?php } ?>
二、添加新博客add.php
<?php include 'conn.php'; // 引入鏈接數據庫 if (! empty ( $_POST ['sub'] )) { $title = $_POST ['title']; $con = $_POST ['con']; $sql = "insert into `news` (`id`,`title`,`dates`,`contents`) values (null,'$title',now(),'$con')"; mysql_query ( $sql ) or die ( "插入失敗" ); echo "插入成功"; } ?> <form action="add.php" method="post"> 標題<input type="text" name="title"><br> 內容 <textarea rows="5" cols="50" name="con"></textarea> <br> <input type="submit" name="sub" value="發表"> </form>
三、刪除博客del.php
<?php include 'conn.php'; if(!empty($_GET['del'])) { $d=$_GET['del']; $sql="DELETE FROM `news` WHERE `id`='$d'"; mysql_query($sql); echo "刪除成功"; }
四、鏈接數據庫conn.php
<?php @mysql_connect ( "localhost:8889", "root", "123456" ) or die ( "mysql鏈接失敗" ); @mysql_select_db ( "php100" ) or die ( "db鏈接失敗" ); // @mysql_set_charset("gbk"); mysql_query ( "SET NAMES UTF8" ); ?>
3、後記:
一、中間遇到了鏈接數據庫時候,MAMP PRO的數據庫和本地自帶的數據庫衝突;
二、MAMP PRO 網絡訪問要設置爲from other computers.