1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Mysql query in a model

Discussion in 'XenForo Development Discussions' started by bambua, Oct 27, 2010.

  1. bambua

    bambua Well-Known Member

    Having a bit of a problem with the following class:

    Code:
    <?php
    class RecentStatus_Model_StatusList
    {
    	public static function getStatusArray() {
    		$db = XenForo_Application::get('db');
    		$statusList = $db->fetchAll("SELECT * FROM xf_news_feed WHERE action = 'status' ORDER BY event_date DESC LIMIT 10",10);
    		return $statusList;
    	}
    }
    
    When I load the page I get the following error:

    Code:
    Server Error
    
    mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement
    
    XenForo_Application::handlePhpError()
    mysqli_stmt->bind_param()
    call_user_func_array() in Zend/Db/Statement/Mysqli.php at line 204
    Zend_Db_Statement_Mysqli->_execute() in Zend/Db/Statement.php at line 312
    Zend_Db_Statement->execute() in Zend/Db/Adapter/Abstract.php at line 468
    Zend_Db_Adapter_Abstract->query() in Zend/Db/Adapter/Abstract.php at line 706
    Zend_Db_Adapter_Abstract->fetchAll() in RecentStatus/Model/StatusList.php at line 6
    RecentStatus_Model_StatusList::getStatusArray() in RecentStatus/Controller/Public.php at line 10
    RecentStatus_Controller_Public->actionIndex() in XenForo/FrontController.php at line 303
    XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
    XenForo_FrontController->run() in /home/ss/public_html/dev/forums/index.php at line 15[SIZE=13px]
    [/SIZE]

    Any ideas? I've been banging my head against this for about an hour, I'm a bit new to the MVC thing but very experienced on the PHP end and just can't find where I'm going wrong.

    Thanks!
     
  2. Paul M

    Paul M Well-Known Member

    You have supplied a parameter of 10, but you have no corresponding ? in the mysql code.

    As an aside, I think there is an inbuilt method for limiting the records so you dont use LIMIT in your actual SQL.
     
  3. bambua

    bambua Well-Known Member

    Holy heck I can't believe I missed that, my eyes literally just scanned right over it. Thanks Paul.
     
  4. Sniper

    Sniper Active Member

    I think it shoud be

    PHP:
    $statusList $db->fetchAll("SELECT * FROM xf_news_feed WHERE action = 'status' ORDER BY event_date DESC LIMIT 10");
    or

    PHP:
    $statusList $db->fetchAll("SELECT * FROM xf_news_feed WHERE action = 'status' ORDER BY event_date DESC LIMIT ?",10);
     
  5. Paul M

    Paul M Well-Known Member

    It believe it should actually be ;

    PHP:
    $statusList $db->fetchAll$db->limit"SELECT * FROM  xf_news_feed WHERE action = 'status' ORDER BY event_date DESC"10 )  );
    or

    PHP:
    $var 10;
    .
    .
    $statusList $db->fetchAll$db->limit"SELECT * FROM  xf_news_feed WHERE action = 'status' ORDER BY event_date DESC"$var )  );
     
  6. Sniper

    Sniper Active Member

    Its a matter of preference really, personally I like;

    PHP:
    $db->select()->from('xf_news_feed''*')->where('action = ?''status')->order('event_date DESC')->limit(10);
     
  7. bambua

    bambua Well-Known Member

    Thanks guys, appreciate the help :D
     
  8. Kier

    Kier XenForo Developer Staff Member

    Any reason you're not extending XenForo_Model for your Model class?
     
  9. bambua

    bambua Well-Known Member

    I actually am in the finished version, I didn't realize I wasn't at first :D
     
    Kier likes this.
  10. Paul M

    Paul M Well-Known Member

    Well thats certainly a new one on me. Is that syntax actually used in XF ?
     
  11. Kier

    Kier XenForo Developer Staff Member

    That syntax may be in Zend Framework, there's all sorts of stuff in there we don't use because at some point we'd like to remove it.
     
  12. Sniper

    Sniper Active Member

    Nope not used, however its part of the Zend Framework as Kier metioned! imho it would be a major loss if removed!
     
  13. ragtek

    ragtek Guest

    So you'd like to remove ZF completly in the feature?
     
  14. Kier

    Kier XenForo Developer Staff Member

    Maybe. It's not a massively high priority though.
     
  15. ragtek

    ragtek Guest

    HM, this would be IMHO a bad move which would end with many not working add-ons because of dependencies and Type Hinting
    For example
    PHP:
    public function match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
    wouldn't work anymore because $request wouldn't be a instance of Zend_Controller_Request anymore, or think of new methods which are doing the same things but have now a new methodname....


    OK, this is still beta, but there are many live sites which are using Add-ons.
    They would have to wait for upgrades o the add-ons until they can move to the new version.
    (we saw this allready after vB4 was released => many didn't upgrade because they're still waiting for a vB4 Version of a important add-on)


    Maybe you could implement ASAP "alias classes" which are only extending the Zend Class, but don't do anything else.
    For example:
    XenForo_Controller_Request extends Zend_Controller_Request
    {
    }
    then the type hinting would work

    class Foo
    {
    function baz(XenForo_Controller_Request $request)
    }
     
  16. Sniper

    Sniper Active Member

    imho either remove Zend before final release or leave it in! Zend is on of the (probably the biggest) reasons I like working with xenForo! Many useful libraries to help with development e.g. the database class. I'm a big Ruby on Rails user so love the database methods provided by Zend.
     
  17. ragtek

    ragtek Guest

    Me too.
    It makes coding Add-ons much easier, because ZF provides many usefull classes which we don't have to implement ourself.
     
  18. Pepelac

    Pepelac Well-Known Member

    please, leave ZF in XenForo, why to invent bicycle, if it has been already invented :)
     
  19. Brogan

    Brogan XenForo Moderator Staff Member

    There's always a better bicycle along at some point ;)
     
  20. Sniper

    Sniper Active Member

    that's debatable :)
     
    ragtek likes this.

Share This Page