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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)