zoukankan      html  css  js  c++  java
  • php第十三节课

    查询

    <?php

    class DBDA
    {
    public $host = "localhost"; //数据库地址
    public $uid = "root"; //数据库用户名
    public $pwd = "123"; //数据库密码

    //执行SQL语句,返回相应的结果的方法
    //参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库
    public function Query($sql,$type=0,$db="mydb")
    {
    //1.造连接对象
    $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db);
    //2.判断连接是否出错
    !mysqli_connect_error() or die("连接失败!");
    //3.执行SQL语句
    $result = $dbconnect->query($sql);

    if($type==0)
    {
    return $result->fetch_all();
    }
    else
    {
    return $result;
    }
    }
    }

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>

    <body>
    <h1>汽车查询页面</h1>
    <br />
    <?php
    include("./DBDA.class.php");
    $db = new DBDA();

    $cx="";
    $value="";
    if(!empty($_POST["name"]))
    {
    $name = $_POST["name"];
    $cx = " where Name like '%{$name}%'";//查询字符串
    $value = $name;
    }
    ?>
    <form action="test.php" method="post">
    <div>
    请输入名称:<input type="text" name="name" value="<?php echo $value; ?>" /> &nbsp;
    <input type="submit" value="查询" />
    </div>
    </form>
    <br />
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
    <td>代号</td>
    <td>汽车名称</td>
    <td>价格</td>
    <td>油耗</td>
    <td>功率</td>
    </tr>

    <?php

    $sql = "select * from Car".$cx;
    $attr = $db->Query($sql);

    foreach($attr as $v)
    {
    //处理Name
    $rp = "<span style='color:red'>{$value}</span>";
    $str = str_replace($value,$rp,$v[1]);
    echo "<tr>
    <td>{$v[0]}</td>
    <td>{$str}</td>
    <td>{$v[7]}</td>
    <td>{$v[4]}</td>
    <td>{$v[5]}</td>
    </tr>";
    }

    ?>

    </table>

    </body>
    </html>

  • 相关阅读:
    QueryString传值的加密与解密方法 .
    正則表達式
    下載模板
    SQL使用存儲過程訪問不同服務器
    asp.net 操作INI文件的读写,读写操作本地ini配置文件
    JS 頁面實時更新時間
    JS 定時刷新父類頁面
    Domino Web中隐藏附件选择框
    Domino移动Web上传的附件到RichText域
    Request的属性和防止图片被盗链
  • 原文地址:https://www.cnblogs.com/xiongxiaobai/p/5465082.html
Copyright © 2011-2022 走看看