一、数据库函数:
mysql_connect(数据库地址,用户名,密码)
mysql_select_db(数据库名)
mysql_set_chartset(‘编码’) //PHP5.2.3以后的函数
mysql_query(“set names 'gbk'”)
函数:
mysql_query(SQL语句或命令)
mysql_fetch_array(资源数据) //返回下标与键名数组
mysql_fetch_row(资源数据) //返回下标数组
mysql_fetch_object(资源数据) //返回对象形式调用
mysql_num_rows(数据资源)
mysql_close(数据资源)
二、微博客实现:
1、首页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 } ?>
2、添加新博客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>
3、删除博客del.php
<?php include 'conn.php'; if(!empty($_GET['del'])) { $d=$_GET['del']; $sql="DELETE FROM `news` WHERE `id`='$d'"; mysql_query($sql); echo "删除成功"; }
4、连接数据库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" ); ?>
三、后记:
1、中间遇到了连接数据库时候,MAMP PRO的数据库和本地自带的数据库冲突;
2、MAMP PRO 网络访问要设置为from other computers.