• php访问数据库


    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>

    页面浏览

  • 相关阅读:
    面向对象的静态属性和静态方法
    面向对象魔术方法及类的自动加载
    面向对象
    mysql cmd 创表查表练习
    创建表 查询表以及添加数据
    windows cmd命令
    4.20 mysq数据库 创建表
    cmd控制数据库初步了解
    Jquery初步了解
    故宫博物院项目 JS功能整理
  • 原文地址:https://www.cnblogs.com/xs-yqz/p/4945016.html
Copyright © 2020-2023  润新知