无法准备查询并且没有错误输出

时间:2013-05-14 22:08:30

标签: php mysqli

今天我的脚本遇到了一些问题,我无法准备查询,脚本也没​​有回复任何错误:

表结构(oops错误表,更正):

CREATE TABLE IF NOT EXISTS `razorphyn_support_users` (
`id`                BIGINT(11)    UNSIGNED    NOT NULL AUTO_INCREMENT,
`name`              VARCHAR(50)               NOT NULL,
`mail`              VARCHAR(50)               NOT NULL,
`password`          VARCHAR(200)              NOT NULL,
`reg_key`           VARCHAR(260)              NOT NULL,
`tmp_password`      VARCHAR(31)               NULL,
`ip_address`        VARCHAR(50)               NOT NULL,
`status`            ENUM('0','1','2','3','4') NOT NULL    DEFAULT '3',
`holiday`           ENUM('0','1')             NOT NULL    DEFAULT '0',
`assigned_tickets`  INT(5)        UNSIGNED    NOT NULL    DEFAULT 0,
`solved_tickets`    BIGINT(11)    UNSIGNED    NOT NULL    DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY(`mail`)) 
ENGINE=MyISAM
DEFAULT CHARSET=utf8 
AUTO_INCREMENT=55;

无法预测的字符串:

$query = "SELECT `id` FROM ".$SupportUserTable." 
WHERE `status`='2' AND `holiday`='0' AND MIN(`assigned_tickets`) 
ORDER BY `solved_tickets` ASC" ;

完整代码(实际上这只是其中的一部分,$stmt已经启动并正常工作,基本上它已经连接了):

file_put_contents('ok.txt','');
$query = "SELECT `id`  FROM ".$SupportUserTable." 
          WHERE `status`='2' AND `holiday`='0' AND MIN(`assigned_tickets`) 
          ORDER BY `solved_tickets` ASC" ;
$prepared = $stmt->prepare($query);
if($prepared){
file_put_contents('ok2.txt','');
if($stmt->execute()){
    file_put_contents('ok3.txt','');
    $stmt->store_result();
    $result = $stmt->bind_result($id);
    file_put_contents('eafv.txt','id: '.$id);
    if($stmt->num_rows>0){
        $query = "UPDATE ".$SupportTicketsTable." SET operator_id=? 
                  WHERE id=? ";
        if($prepared = $stmt->prepare($query)){
            if($stmt->bind_param('ii', $id,$tkid)){
                if($stmt->execute()){
                    echo json_encode(array(0=>'Created'));
                }
                else
                    echo json_encode(array(0=>mysqli_stmt_error($stmt)));
            }
            else
                echo json_encode(array(0=>mysqli_stmt_error($stmt)));
        }
        else
            echo json_encode(array(0=>mysqli_stmt_error($stmt)));
    }
    else
        echo json_encode(array(0=>'No Operator Available'));
}
else
    echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
    echo json_encode(array(0=>$stmt->error));

1 个答案:

答案 0 :(得分:2)

您不能将MIN()表达式放在SQL的WHERE子句中。您只能将MIN()表达式放在select-list,HAVING子句和ORDER BY子句中。

你想要的是:

  

我想选择具有最小开票数量的运营商,如果有多个运营商具有相同的价值[然后]选择具有较少解决票证的运营商

以下是获得此结果的解决方案:

SELECT `id` FROM razorphyn_support_users 
WHERE `status` = 2 AND `holiday` = 0 
ORDER BY `assigned_tickets` ASC, `solved_tickets` ASC
LIMIT 1

重新评论:

对于LIMIT语法,请参阅https://stackoverflow.com/a/3325580/20860或MySQL手册,其中包含:

  

为了与PostgreSQL兼容,MySQL还支持LIMIT row_count OFFSET偏移语法。

如果类型是字符串或枚举,则使用字符串分隔符很好。我假设列是整数(我没有仔细检查上面的表定义),并删除了字符串分隔符,因为它们不是整数所必需的。