XF 2.0 Problem with function. Why $db->fetch not works in this case?

karaulov

New member
Hello. I tried to write code to get 'Maps' by some parameter with single function.
But faced with an almost insurmountable obstacle.



Not works:
PHP:
    public static function getMapsCountByParam( $param, $val )
    {
        $db = \XF::db();
        $map = $db->fetchOne("SELECT COUNT(id) FROM maplist WHERE ? = ?",["user_id",$val]);
        return $map;
    }
...
getMapsCountByParam("user_id",$visitor->user_id);

Not works:
PHP:
    public static function getMapsCountByParam( $param, $val )
    {
        $db = \XF::db();
        $map = $db->fetchOne("SELECT COUNT(id) FROM maplist WHERE ? = ?",[$param,$val]);
        return $map;
    }
...
getMapsCountByParam("user_id",$visitor->user_id);


Only this function works:

PHP:
    public static function getMapsCountByParam( $param, $val )
    {
        $db = \XF::db();
        $map = $db->fetchOne("SELECT COUNT(id) FROM maplist WHERE user_id = ?",[$val]);
        return $map;
    }
...
getMapsCountByParam("user_id",$visitor->user_id);


How to use '?' after 'WHERE' ?

This is bug or what ?
 
Top Bottom