douwei4370 2014-05-31 10:18
浏览 24
已采纳

从表中选择*,其中名称为%O%

In phpMyAdmin the query

SELECT * FROM table WHERE value1 LIKE '%O%' OR value2 LIKE '%1%'

nicely returns all rows containing O or 1.

But the same query implemented in a PHP script only returns the first row matching O or 1. Instead off all rows matching O or 1.

This is my current PHP script:

mysqli_select_db ($con, $database) or die (mysqli_error());

            $sqli = mysqli_query ($con,"SELECT * FROM table WHERE VALUE1 LIKE '%O%' OR VALUE2 LIKE '%1%'") or die (mysqli_error()); 

            $echo = mysqli_fetch_assoc($sqli);

            if (empty ($echo))
            {
                echo "No matching results found.";
            }
            else
            {
                echo "<table width='50%' border='1'>";
                echo "<tr><th>VALUE1</th><th>VALUE2</th></tr>";
                echo "<tr><td>{$echo["VALUE1"]}</td>";
                echo "<td>{$echo["VALUE2"]}</td></tr>";
            }

The output is only the first matching row. Instead of all matching rows. Also when I use print_r instead of table rows.

  • 写回答

1条回答 默认 最新

  • doo6568 2014-05-31 10:23
    关注

    You have to iterate through all the rows with a loop like this

    if( !mysqli_num_rows($sqli) )
    {
        echo "No matching results found.";
    } else {
        $table = "<table width='50%' border='1'>
    ";
        $table .= "<tr><th>VALUE1</th><th>VALUE2</th></tr>
    ";
        while( $echo = mysqli_fetch_assoc($sqli) )
        {
            $table .= "<tr><td>{$echo['VALUE1']}</td><td>{$echo['VALUE2']}</td></tr>
    ";
        }
        $table .= "</table>";
    }
    

    Note I added a couple of lines of code to count the rows and display a message if there are no matches found (as I intended is your goal from your question).


    Edit: Better would be to use the built in num_rows instead of reinventing the wheel


    Edit 2: I have a feeling he didn't want the table to duplicate for each row. Change the code a bit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错