如何检查字符串是否存在于X行以上

时间:2015-03-31 21:44:42

标签: php mysql

我想知道以下是否可行,以及如何:

我想检查我的表格中的用户名是否显示在5行或更多行中

我的桌子:

enter image description here

我想要的用法示例:

如果名称(teste)超过5行,则执行某些操作..

1 个答案:

答案 0 :(得分:2)

使用HAVING,它允许您按聚合/操作字段进行过滤

SELECT count(username) as shows_counter from tbl 
group by username 
HAVING count(username)>5


$check = mysql_query("SELECT count(name),name 
FROM hack_log group by name HAVING count(name)>5"); 

$check = mysql_num_rows($result);// **Add this row**

if($check){ echo "HACKER!"; }else{ echo "not hacker"; }
  • 请使用PDO或mysqli内置对象

满足您的需求

您需要检查行数

    $db = new PDO('mysql:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);
    $q = $db->prepare('SELECT count(name),name 
    FROM hack_log group by name HAVING count(name)>5');
    $q->execute();
    $rows = $q->fetchAll();

   $res= count($rows)>0?"Hacker":"No Hacker";
    echo $res;