PDO选择最大查询不返回任何内容,空白页

PDO选择最大查询不返回任何内容,空白页

问题描述:

So I have a function within a class to take the highest value based on another value but I'm getting a blank page.

I checked for bad syntax but there's none that the validator found and if I change it to header("Location: https://facebook.com") it will go to the site so it seems the function is working. However I am not getting my variable back.

My connections and everything else are fine as my other functions in this class return what I want.

class FORUM
{
    private $forum;

    function __construct($DBFORUM_con)
    {
        $this->db = $DBFORUM_con;
    }

    public function addCount($id)
    {
        try
        {   
            $stmt = $this->db->prepare("SELECT MAX(a_id) AS Maxa_id FROM forum_answer WHERE question_id = :id");

            $stmt->execute(array(':id'=>$id));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($stmt->rowCount() > 0)
            {
                $Max_id = $userRow['Maxa_id'] + 1;

                return $Max_id;
            }
            else {
                $Max_id = 1;

                return $Max_id;
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}

Then in my page that processes the form:

//require class

$id = $_POST['id'];

if ($forum->addCount($id))
{
    echo $Max_id;
}
else
{
    echo $Max_id;
}

所以我在一个类中有一个函数来根据另一个值取最高值但是我得到一个空白 页面。 p>

我检查了错误的语法,但验证器找不到,如果我将其更改为 header(“Location:https://facebook.com”) 代码>它会进入网站,所以看起来功能正常。 但是我没有得到我的变量。 p>

我的连接和其他一切都很好,因为我在这个类中的其他函数返回我想要的东西。 p>

   class FORUM 
 {
 private $ forum; 
 
 function __construct($ DBFORUM_con)
 {
 $ this-> db = $ DBFORUM_con; 
} 
 
公共函数addCount(  $ id)
 {
尝试
 {
 $ stmt = $ this-> db-> prepare(“SELECT MAX(a_id)AS Maxa_id FROM forum_answer WHERE question_id =:id”); 
 
  $ stmt-> execute(array(':id'=> $ id)); 
 $ userRow = $ stmt-> fetch(PDO :: FETCH_ASSOC); 
 
 if($ stmt->  rowCount()> 0)
 {
 $ Max_id = $ userRow ['Maxa_id'] + 1; 
 
返回$ Max_id; 
} 
其他{
 $ Max_id = 1; 
 \  n返回$ Max_id; 
} 
} 
 catch(PDOException $ e)
 {
 echo $ e-> getMessage(); 
} 
} 
} 
  code>   
 
 

然后在我处理表单的页面中: p>

  // require class 
 
 $ id = $ _POST ['id']; \  n 
if($ forum-> addCount($ id))
 {
 echo $ Max_id; 
} 
else 
 {
 echo $ Max_id; 
} 
  code>  pre  > 
  div>

You're not assigning the return value to anything.

Try

$id = $_POST['id'];

$Max_id = $forum->addCount($id)

if ($Max_id)
{
    echo $Max_id;
}
else
{
    echo $Max_id;
}