从php类查询mysql

从php类查询mysql,php,mysql,class,Php,Mysql,Class,我正在从一个php类内部执行这样的查询 <?php require_once('abstractTable.php'); class User_Orders extends AbstractTable{ public static function get_by_month($month, $year){ $query = "select * from `user_orders` where month(`date_order`) = '2' and year(`date_order`)

我正在从一个php类内部执行这样的查询

<?php
require_once('abstractTable.php');
class User_Orders extends AbstractTable{
public static function get_by_month($month, $year){
$query = "select * from `user_orders` where month(`date_order`) = '2' and year(`date_order`) = '2012'";
return self::getObjectList(self::$class, $query);
}
?>
当脚本执行时,我得到如下错误消息:

错误:您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获取第1行“”附近要使用的正确语法

我的问题是,是否允许将查询放入这样的类中

谢谢

这是“允许的”。尝试从控制台运行它

试试这个:

$query = "select * from user_orders where month(date_order) = 2 and year(date_order) = 2012;";

据我所知,month和year函数返回一个整数类型,因此请尝试删除'2'和'2012'中的引号。

试试这个

$query = "select * from user_orders where month(date_order)=2 and year(date_order)=2012";

它应该可以工作…

每当我遇到此类错误时,我都会在
phpmyadmin
中运行查询,以检查是查询的错误还是我的php代码的错误。为什么不直接对SQL server测试查询,看看到底是什么错误?查询没有问题。它在sql server中工作。只是当我运行php脚本时,我不断地发现这个错误。显示
getObjectList()
的内容我想它可能在这个问题上起到了一定作用。你能发布
getObjectList
代码吗?或者最好是整个classMysql为你做这件事。因此,不必担心这个问题,因为查询是有效的。我认为问题在于我将查询放在php类中的方式。也许。为了调试这个问题,您可以在打印错误时打印
$q
变量,以查看您正在运行的实际查询与我之前发布的答案有什么区别?@MostyMostacho:Point 1:您有
在结尾处使用双重代码,我认为这不起作用。。。第二点:我甚至没有复制你的查询。。明白了吗???我刚看了你的问题。。。第三点:我已经起草了这个问题,但是由于互联网的不连通,我发的很晚。。。
$query = "select * from user_orders where month(date_order)=2 and year(date_order)=2012";