php - 带有 order by rand 函数的 where 语句不起作用

标签 php mysql mysqli

我有一个包含三类潜在客户的数据表

  1. 热门
  2. 未指定

现在我想做的是,当用户点击“Hot Leads”时,系统将从“HOT”下的潜在客户数据类别中获取 5 个随机记录。

下面是我的代码:

<?php
session_start();
include 'db_connect.php';
require 'logincheck.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$username = mysqli_real_escape_string($conn, $_SESSION['uiduser']);
$fullname = mysqli_real_escape_string($conn, $_SESSION['fullname']);

if(isset($_GET["q"])) {


// posted data
$query_q = mysqli_real_escape_string($conn, $_GET['q']);
$status="Fresh";

$sql = "SELECT * FROM adv_leads WHERE adv_type='".$query_q."' AND 
adv_status='".$status."' ORDER BY RAND() LIMIT 5";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 0){}else{ echo "error";}


while($build = mysqli_fetch_assoc($result))
{ 
echo '<tr class="select">';
              echo '<td>'.$build['adv_uid_leads'].'</td>';
              echo '</tr>';
}}
?>

这里

q= Type of lead i.e.HOT, COLD
Status= Fresh Leads

如果我删除 WHERE 子句,系统工作正常,但使用 WHERE 子句,它会返回“0”结果。

最佳答案

friend 我可以知道你的数据库的值(value),因为每当你使用 "adv_type='".$query_q."'" ,它将检查完全匹配,而不是您可以在查询中使用 like

例如:

 $sql = "SELECT * FROM adv_leads WHERE adv_type LIKE'%".$query_q."%' AND 
adv_status='".$status."' ORDER BY RAND() LIMIT 5";

关于php - 带有 order by rand 函数的 where 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49468022/

相关文章:

php - 正则表达式匹配列表中的字符,除非存在另一个列表中的字符

javascript - php到node.js ajax发送没有给出响应

mysql - mysql服务器和客户端协议(protocol)中的 "affected rows"和 "last_insert_id"有什么用?

php - 在 MySQL DB 中存储翻译的最佳方式

php - 编写 MySQLI 调用的更好方法

php - 将 MySQL 结果保存在二维数组中 - php

php - 使用PHP连接MySQL : ERROR?

php - 检索特定 ID 一次

php - Symfony 和 Doctrine 使迁移无效

php - 使用多个选择器删除一条mysql记录