简体   繁体   English

如何计算ID匹配的行?

[英]How do I count rows with matching id?

I'm currently having some difficulty with determining if any rows match a specific user id. 我目前在确定是否有任何行与特定用户ID匹配时遇到一些困难。 The code runs fine, but the if statement (if($notifs !== false) always returns true and hence "No notifications found" never displays. How do I write the statement to only check for "receive_id" that match the current session id? 该代码运行良好,但是if语句(if($ notifs!== false)始终返回true,因此从不显示“未找到通知”。我如何编写该语句以仅检查与当前会话匹配的“ receive_id” ID?

$user_id = $_SESSION['userID'];

if(isset($_POST["view"]))
{

$stmt = $user_notif->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$user_id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$stmt = $user_notif->runQuery("SELECT * FROM notif_follow WHERE receive_id= ? ORDER BY id DESC LIMIT 5");
$stmt->bindValue(1,$user_id);
$stmt->execute();
$notifs = $stmt->fetchAll(PDO::FETCH_ASSOC);

$notification = '';

if($notifs !== false)
 {
  foreach($notifs as $notif)
  {

  $send_id = $notif['send_id'];

  $query2 = $user_notif->runQuery("SELECT id FROM following WHERE user1_id=:uid1 AND user2_id=:uid2");
  $query2->execute(array(":uid1"=>$user_id,":uid2"=>$send_id));
  $query2result = $query2->fetch(PDO::FETCH_ASSOC);

  if($query2result !== false){
  $follow .= '<a href="followoff.php?id='.$send_id.'"><button class="button">Remove Channel</button></a>';
  }
  else{
  $follow .= '<a href="followon.php?id='.$send_id.'"><button class="button">Add Channel</button></a>';
  }

  $notification .= '
   <li>
     <div class="notifbox">
     <strong style="color: #4b8ed3;">'.$notif["send_name"].'</strong><p style="color: #fff;"> has added you.</p>

     '.$follow.'

&nbsp<a href="index.php?id='.$notif["send_id"].'"><button class="button">View Channel</button></a>
     </div>
   </li>
   <div class="sectionheader3"></div>
   ';

  }
 }
 else
 {
  $notification .= '<li><h2 style="color: #4b8ed3; padding: 10px;">No Notifications Found<h2></li>';
 }

http://php.net/manual/en/pdostatement.fetchall.php says: http://php.net/manual/en/pdostatement.fetchall.php说:

An empty array is returned if there are zero results to fetch, or FALSE on failure. 如果要获取的结果为零,则返回空数组;如果失败,则返回FALSE。

A query that matches zero rows is not a failure. 匹配零行的查询并非失败。 It's a success at giving you the information that there are zero matching rows. 成功地为您提供了零匹配行的信息。

So you shouldn't compare with $notifs !== false , which compares exactly to the boolean false . 因此,您不应与$notifs !== false进行比较,后者恰好与布尔值false进行比较。 You might like to compare with: 您可能要与以下内容进行比较:

if (!$notifs)

This will also test for the empty array. 这还将测试空数组。

fetchAll only returns false if the query failed, but it's not failing. 如果查询失败,则fetchAll仅返回false ,但不会失败。 The result is an array. 结果是一个数组。 Your best to check if count($notifs) > 0 and also if $notifs === false for the possibility of the query failing. 最好检查count($notifs) > 0以及$notifs === false是否存在查询失败的可能性。

Try 尝试

if ($stmt->RowCount() > 0) {
$notifs = $stmt->fetchAll(PDO::FETCH_ASSOC);
//code here
}

Instead of 代替

$notifs = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($notifs !== false) {
//code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table 如何根据ID列(准备好的语句)对表中的行进行计数? - How do I count rows in a table according to ID column (prepared statements)? 如何计算返回的行数? - How do I count the number of rows returned? 如何在SQL中选择和计数行? - How do I SELECT and COUNT rows in SQL? 如何计算查询结果行数? - How do I count query result rows? 如果当前列“Id”与 Codeignitor PHP 中的另一个表匹配,如何计算行数? - How to Count rows If Current Column `Id` is Matching from Another Table in Codeignitor PHP? 我如何需要返回查询以计算行数,以及如何计算行数? - How do I need to return the query in order to count the number of rows, and how do I count the rows? 如何使用嵌套 SELECT 获取 INSERT 查询的插入行数? 没有受影响的行,结果 ID,数量行 - How do I get a count on inserted rows for INSERT query with nested SELECT? No luck with affected_rows, result_id, num_rows 如何计算laravel组中唯一行的数量? - How do I count the number of unique rows in group by laravel? 如何计算MySQL表中的行数? - How do I count the number of rows in a MySQL Table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM