dpus81500574 2011-07-01 21:36
浏览 21

显示2个随机行而不是一行

MY SQL QUERY:

$q = mysql_query("SELECT * FROM `ads` WHERE keywords LIKE '%$key%' ORDER BY RAND()");

RESULTS: KEYWORD123

This query searches and results in one random row but i want to show 2 random rows. How to do that?

any solution?

how??

im grabbing it using this

$row = mysql_fetch_array($q); if ($row <= 0){ echo 'Not found'; }else{ echo $row['tab']; }

  • 写回答

4条回答 默认 最新

  • dongrong6235 2011-07-01 21:39
    关注

    That query (as-is) will return more than one row (assuming more than one row is LIKE %$key%). If you're only seeing one record, it's possible you're not cycling through the result set, but rather pulling the top response off the stack in your PHP code.

    To limit the response to 2 records, you would append LIMIT 2 onto the end of the query. Otherwise, you'll get every row that matches the LIKE operator.

    //Build Our Query
    $sql = sprintf("SELECT tab 
                    FROM ads 
                    WHERE keyword LIKE '%s' 
                    ORDER BY RAND() 
                    LIMIT 2", ('%'.$key.'%'));
    // Load results of query up into a variable
    $results = mysql_query($sql);
    // Cycle through each returned record
    while ( $row = mysql_fetch_array($result) ) {
      // do something with $row
      echo $row['tab'];
    }
    

    The while-loop will run once per returned row. Each time it runs, the $row array inside will represent the current record being accessed. The above example will echo the values stored in your tab field within your db-table.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大