• 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

bambua

Well-known member
#1
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!
 

Paul M

Well-known member
#2
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.
 

Sniper

Active member
#4
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);
 

Paul M

Well-known member
#5
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 )  );
 

Sniper

Active member
#6
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);
 

Paul M

Well-known member
#10
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);
Well thats certainly a new one on me. Is that syntax actually used in XF ?
 
R

ragtek

Guest
#15
Maybe. It's not a massively high priority though.
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($routePath, Zend_Controller_Request_Http $request, XenForo_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)
}
 

Sniper

Active member
#16
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.
 
R

ragtek

Guest
#17
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
Me too.
It makes coding Add-ons much easier, because ZF provides many usefull classes which we don't have to implement ourself.