码迷,mamicode.com
首页 > 数据库 > 详细

php访问数据库

时间:2015-11-07 15:55:38      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:

1.创建数据库`lyb`

CREATE DATABASE `lyb`;

CREATE TABLE  `lyb1` (
id INT NOT NULL AUTO_INCREMENT ,
title VARCHAR( 200 ) NOT NULL ,
content TEXT NOT NULL ,
author VARCHAR( 30 )not null DEFAULT  彭军,
email VARCHAR( 40 ) not null DEFAULT  1098325951@qq.com,
PRIMARY KEY ( id ) ,
UNIQUE (title)
) ENGINE = INNODB DEFAULT CHARSET = utf8

数据库连接文件conn.php

header("Content-type: text/html; charset=UTF-8"); 
 $DB_SERVER = "localhost";
 $DB_NAME = "root";
 $DB_PWD = "";
 $conn = mysql_connect($DB_SERVER,$DB_NAME,$DB_PWD);
 if (!$conn) {
     die("连接数据库失败".mysql_errno());    
  }
  mysql_query("set names utf8");

2.建立前台页面 5.1.php

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 13:50:48
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8");  

 if (isset($_POST[submit])) {
     require("include/conn.php");
 mysql_select_db("lyb",$conn);//选择数据库

 $title = $_POST[title];
 $author = $_POST[author];
 $content = $_POST[content];
 $email = $_POST[email];
 $result = mysql_query("insert into `lyb1`(`id`,`title`,`content`,`author`,`email`) values(null,‘$title‘,‘$content‘,‘$author‘,‘$email‘)");
 var_dump($result);
 mysql_free_result($result);
 mysql_close($result);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加数据页面</title>
</head>
<body>
    <form action="5.1.php" method="POST">
        <p>添加新闻页面</p>
        <div>标题:<input type="text" name="title" id=""></div>
        <div>内容:    <textarea  name="content" id="" cols="30" rows="5"></textarea></div>
        <div>作者:<input type="text" name="author" id=""></div>
        <div>邮箱:<input type="text" name="email" id=""></div>
        <div><input type="reset" value="重置"><input type="submit" name="submit" value="提交"></div>
    </form>
</body>
</html>

3.从数据库中获取数据显示在前台页面

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 13:40:08
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//选择数据库
 $result = mysql_query("select * from `lyb1`",$conn);//选择数据库表
 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>新闻显示页面</title>
 </head>
 <body>
     <table border="1">
         <tr bgcolor="#ccc">
             <th>序号</th>
             <th>标题</th>
             <th>内容</th>
             <th>作者</th>
             <th>邮箱</th>
         </tr>
         <?php
         while ($row = mysql_fetch_assoc($result)) {
             ?>
             <tr>
                 <td><?= $row[id]?></td>
                 <td><?= $row[title]?></td>
                 <td><?= $row[content]?></td>
                 <td><?= $row[author]?></td>
                 <td><?= $row[email]?></td>
             </tr>
             <?php        
         }
         ?>
     </table>
     <p>共有<?= mysql_num_rows($result) ?>条记录 </p>
      <!-- mysql_num_rows()函数返回的是结果集的总数 -->
     <?php
     //释放资源,关闭结果集
     mysql_free_result($result);
     mysql_close($result);
     ?>
 </body>
 </html>

页面浏览

技术分享

 

技术分享

php访问数据库

标签:

原文地址:http://www.cnblogs.com/xs-yqz/p/4945016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!