php连接MySQL数据并做模糊查询

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
        <input type="text" name="keyword">
        <button>搜索</button>
    </form>
    <?php 

        // $min = 0;
        $maxs = 10000;
        $sizePage = 10;
        $currentPage = 1;
        $keyWord = $_GET['keyword'];
        echo "<h1>".$keyWord."</h1>";
        // 创建连接
        $conn = new mysqli('127.0.0.1', 'root', 'root', 'test1');
        if(!$conn){
            echo '连接失败', mysqli_connect_error(), "<br/>";
        }
        else{
            echo '连接成功', "<br />";
        }
        // $res = mysqli_query($conn, 'select * from industry_distribution where id > 0 and id < 21');
        $res = mysqli_query($conn, 'select * from industry_distribution where (label like "%'.$keyWord.'%" or name like "%'.$keyWord.'%") and id < '.$maxs.' ');
        // $rows = mysqli_num_rows($res);
        // $rows = count($res, COUNT_RECURSIVE);
        // echo $rows;
        // for($i=0;$i<$rows;$i++){
        //     $arr=mysqli_fetch_array($res);
        //     echo $arr['id'],$arr['name'];
        // }
        // echo count($res, COUNT_RECURSIVE);

        // echo gettype($res);
        echo '查询到的记录条数:',mysqli_num_rows($res);
        
        // echo var_dump(is_array($res));    // bool(false)

        // echo mysqli_num_rows($res);
        echo '<table border="1">
            <th>id</th>
            <th>name</th>
            <th>company</th>
            <th>indystry</th>
            <th>label</th>
';
            $i = 1;
        while($row = mysqli_fetch_array($res)){
            echo '<tr><td>'. $i .'</td><td>'.$row['name'],'</td><td>', $row['company'],'</td><td>', $row['indystry']."</td><td>",$row['label'],"</td></tr>";
            $i++;
        }
        echo '</table>';

        mysqli_close($conn);
    ?>
</body>
</html>

查询结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#的MySQL连接库来连接MySQL数据库并执行查询操作。首先,你需要安装MySQL连接库,可以使用NuGet包管理器来安装`MySql.Data`库。 下面是一个示例代码,展示如何连接MySQL数据库并查询一条数据: ```csharp using System; using MySql.Data.MySqlClient; namespace MySQLExample { class Program { static void Main(string[] args) { // MySQL连接字符串 string connStr = "server=localhost;user=root;database=testdb;password=your_password"; // 创建MySQL连接对象 MySqlConnection conn = new MySqlConnection(connStr); try { // 打开数据连接 conn.Open(); // 创建查询语句 string sql = "SELECT * FROM your_table LIMIT 1"; // 创建MySQL命令对象 MySqlCommand cmd = new MySqlCommand(sql, conn); // 执行查询并获取结果 MySqlDataReader rdr = cmd.ExecuteReader(); // 读取查询结果 while (rdr.Read()) { // 获取列的值 int id = rdr.GetInt32(0); string name = rdr.GetString(1); // 打印结果 Console.WriteLine("ID: {0}, Name: {1}", id, name); } // 关闭数据阅读器 rdr.Close(); } catch (MySqlException ex) { Console.WriteLine("Error: " + ex.Message); } finally { // 关闭数据连接 conn.Close(); } } } } ``` 请确保替换`connStr`变量中的连接字符串为你自己的MySQL服务器信息。在上面的示例中,我们连接到名为`testdb`的数据库,并从表中选择第一行数据。 这只是一个简单的示例,你可以根据你的具体需求进行修改和扩展。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值