php - 我的 comment_func_bar 功能不起作用

标签 php mysql

我的页面上有评论功能,但我正在尝试向其中添加一些功能。

like-, report- and delete button

但是我的代码似乎没有必要复杂。

    function comment_func_bar($how_many,$comment_id,$uid)
    {
      // connect to the database
    //include_once('config.php');
     $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

     // get the records from the database
     $result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
     $row = mysqli_fetch_row($result);
     if($result->num_rows === 0) //If no likes
     {
        $how_many='';
     } else if(($result->num_rows === 1) && ($row[0] === "$uid")) { //If one like, and that one like is "you"
    $how_many = "You like, "; //Should be $_SESSION["user_name"] or what ever
     }else if($result->num_rows === 1) //If one like, and its not "you"
     {
    $other_user_id=$row[0];
    $result=$mysqli->query("SELECT `user_name` FROM `users` WHERE `user_id` = '".$other_user_id."'");
    $row = mysqli_fetch_row($result);
    $how_many=$row[0];
     }

     //Check if the user already have liked a comment
//http://stackoverflow.com/questions/10164035/how-to-find-specific-row-in-mysql-query-result
    while($row = mysqli_fetch_row($result))
    {
      if($row === $uid)
      {
        //row found, do processing ...
        //reset pointer and break from loop
                echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Unlike</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
        if(!mysql_data_seek($result, 0))
          //Row not found
                  echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Like</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
        break;
      }
    }
    }

但实际的问题是在底部的 while 。

我正在尝试搜索用户 $uid 的行,以了解该用户是否已经喜欢。但我无法让它工作。

它什么也没返回,没有错误,什么也没有!

最佳答案

您实际上可以在代码中找到帮助我解决此问题的线程。 和ofc http://php.net/manual/en/control-structures.if.php

    function comment_func_bar($how_many,$comment_id,$uid)
{
  // connect to the database
//include_once('config.php');
 $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

 // get the records from the database
 $result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
 $row = mysqli_fetch_row($result);

 if($result->num_rows === 0) //If no likes
 {
    $how_many='';
 } else if(($result->num_rows === 1) && ($row[0] === "$uid")) { //If one like, and that one like is "you"
$how_many = "You like, "; //Should be $_SESSION["user_name"] or what ever
 }else if($result->num_rows === 1) //If one like, and its not "you"
 {
$other_user_id=$row[0];
$result=$mysqli->query("SELECT `user_name` FROM `users` WHERE `user_id` = '".$other_user_id."'");
$row = mysqli_fetch_row($result);
$how_many=$row[0];
 }

 //Check if the user already have liked a comment
 //http://stackoverflow.com/questions/4037145/mysql-how-to-select-rows-where-value-is-in-array
 ////http://stackoverflow.com/questions/10164035/how-to-find-specific-row-in-mysql-query-result
// $row = mysqli_fetch_row($result);
$result=$mysqli->query("SELECT `uid_fk` FROM `comment_likes` WHERE `comment_id_fk` = '".$comment_id."'");
  if(!cycelUser($result,$uid))
  {        
            echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Like</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
    
        }else {
            echo '<p id="txtHint"><small><a href="" class="clickable">' . $how_many .' Unlike</a> - <a href="">Report</a> - <a href="#" class="del_button" id="del-'.$comment_id.'">Delete</a></small></p>';
}
  }


// Cycel throu the uid_fk from comment_likes to find if the user have liked it before!
  // Not a very nice solution, but it works (apperently)
function cycelUser ($result,$uid)
{
    while ( $row = mysqli_fetch_assoc( $result ) ) { //_array or _assoc whatever
        if ( $row['uid_fk'] == $uid ) {
            mysqli_data_seek( $result, 0 );
            return $row;
        }
    }
}

关于php - 我的 comment_func_bar 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929390/

相关文章:

php - 模拟 Symfony Ldap::create 用于单元测试

javascript - AngularJS ng :repeat not working

mysql - 如何使用 rails 在 sql 查询的 where 子句中的值数组上生成 LIKE?

mysql - 对于一列来说,排序永远需要花费大量时间

mysql - 在 MySQL 中,UNIQUE 索引会作为 varchar 列的快速查找吗?

php - SQL 查询和 PHP 检查用户是否是管理员

php - .htaccess 与自定义请求 URI 处理程序和 sef 链接

php - Symfony 有 OpenID 2.0 插件吗?

mysql - 估计 mysql 中读/写查询的数量

php - 根据 Woocommerce 中的自定义购物车商品数据更改购物车商品价格