drhgzx4727 2013-01-01 19:50
浏览 94
已采纳

使用双引号字符串选择时,mysql上的错误1064 <42000>

The query produced by Zend Framework 2:

SELECT "uc".*, "c".* FROM "user_contacts" AS "uc" INNER JOIN "contacts" AS "c" ON "uc"."contact_id" = "c"."contact_id" WHERE "uc"."user_id" = '2' AND "c"."user_id" = '1';

results in this error (when run on command line):

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.*, "c".* FROM "user_contacts" AS "uc" INNER JOIN "contacts" AS "c" ON "uc"."con' at line 1

This query (exact same query minus double quotes) runs fine:

SELECT uc.*, c.* FROM user_contacts AS uc INNER JOIN contacts AS c ON uc.contact_id = c.contact_id WHERE c.user_id = 2 AND uc.user_id = 1;

+---------+------------+------------+---------+
| user_id | contact_id | contact_id | user_id |
+---------+------------+------------+---------+
|       1 |          7 |          7 |       2 |
+---------+------------+------------+---------+
1 row in set (0.00 sec)

Why is this so and how can I fix this?

Using AMP stack on Ubuntu 12.10.

Tables look like so:

CREATE TABLE `contacts` (
    `contact_id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) NOT NULL,
    PRIMARY KEY (`contact_id`),
    UNIQUE KEY `contact_id_UNIQUE` (`contact_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `user_contacts` (
    `user_id` int(11) NOT NULL,
    `contact_id` int(11) NOT NULL,
    PRIMARY KEY (`user_id`,`contact_id`),
    KEY `user_contacts_user_id_fkey_idx` (`user_id`),
    KEY `user_contacts_contact_id_idx` (`contact_id`),
    CONSTRAINT `user_contacts_contact_id_fkey` FOREIGN KEY (`contact_id`) REFERENCES    `contacts` (`contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT `user_contacts_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Zend db adapter code:

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=' . $dbName . ';host=' . $host,
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

Select code:

public function checkIfFriends($currentUserId,$requestedUserId) {
    $currentUserId      = (int) $currentUserId;
    $requestedUserId    = (int) $requestedUserId;

    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from(array('uc' => $this->tableGateway->getTable()))
        ->join(array('c' => 'contacts'), 'uc.contact_id = c.contact_id');

    $where = new Where();
    $where
        ->equalTo('uc.user_id', $currentUserId)
        ->equalTo('c.user_id', $requestedUserId);
    $select->where($where);


    //echo $select->getSqlString();
    $rowSet = $this->tableGateway->selectWith($select);
    $row = $rowSet->current();
    return ($row) ? true: false;  
}

Why has this been closed as a exact duplicate? it isn't. I understand the problem might be the same but ZF2 is producing a query which doesn't run due to the quoting.

  • 写回答

2条回答 默认 最新

  • duanpa5237 2013-01-01 20:32
    关注

    Solution as found in this Zend Framework forum post:

    Works but is not really an acceptable solution, a better solution would be much appreciated.

    public function checkIfFriends($currentUserId,$requestedUserId) {
        $currentUserId      = (int) $currentUserId;
        $requestedUserId    = (int) $requestedUserId;
    
        $sql = new Sql($this->tableGateway->getAdapter());
    
        $select = $sql->select();
        $select->from(array('uc' => $this->tableGateway->getTable()))
            ->join(array('c' => 'contacts'), 'uc.contact_id = c.contact_id');
    
        $where = new Where();
        $where
            ->equalTo('uc.user_id', $currentUserId)
            ->equalTo('c.user_id', $requestedUserId);
        $select->where($where);
    
        $dbAdapter = $this->tableGateway->getAdapter();
        $string = $sql->getSqlStringForSqlObject($select);
        $rowSet = $dbAdapter->query($string, $dbAdapter::QUERY_MODE_EXECUTE);
    
        $row = $rowSet->current();
        return ($row) ? true: false;  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页