zend-db不执行pdo绑定替换

时间:2018-02-16 06:15:34

标签: mysql mysqli pdo zend-db

我的Zend Db代码没有执行pdo绑定替换。您可以看到我的数据库的输出:

mysql> select * from nzbsources;
+----+----------+-------+------------+-------+--------------+--------+-------------+-----------+---------+---------+--------+--------------+------------+------------+
| id | parentid | flags | categoryid | hash  | releaseName  | imdbId | srcLinkUri  | srcTitle  | rssUri  | pubdate | status | description  | created    | updated    |
+----+----------+-------+------------+-------+--------------+--------+-------------+-----------+---------+---------+--------+--------------+------------+------------+
|  1 |        0 |     0 |          0 | :hash | :releaseName |      0 | :srcLinkUri | :srcTitle | :rssUri |       0 |      1 | :description | 1518761515 | 1518761515 |
+----+----------+-------+------------+-------+--------------+--------+-------------+-----------+---------+---------+--------+--------------+------------+------------+

这是我的项目数据库连接config xml source:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xi="http://www.w3.org/2001/XInclude">
    <database>
        <driver>Pdo_Mysql</driver>
        <database>nzbscraper</database>
        <username>root</username>
        <charset>utf8</charset>
        <platform>Mysql</platform>
    </database>
</root>

这是我处理数据库查询的PHP代码

// load nzb database adapter
$dbConfigs=$projectConfigsArr['database'];
$dbConfigs['driver_options']=[
    \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
    \PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$dbAdapter=new \Zend\Db\Adapter\Adapter($dbConfigs);
$qi = function ($name) use ($dbAdapter) {
    return $dbAdapter->platform->quoteIdentifier($name);
};
$fp = function ($name) use ($dbAdapter) {
    return $dbAdapter->driver->formatParameterName($name);
};

// insert nzb metadata into database
$dbTableName=$qi('nzbsources');
$sql="INSERT INTO $dbTableName (".$qi('hash').",".$qi('categoryid').",".$qi('releaseName').",".$qi('imdbId').",".$qi('srcLinkUri').",".$qi('srcTitle').",".$qi('rssUri').",".$qi('pubdate').",";
$sql.=$qi('status').",".$qi('description').",".$qi('created').",".$qi('updated').") VALUES ('".$fp('hash')."','".$fp('categoryid')."','".$fp('releaseName')."','";
$sql.=$fp('imdbId')."','".$fp('srcLinkUri')."','";
$sql.=$fp('srcTitle')."','".$fp('rssUri')."','".$fp('pubdate')."','1','".$fp('description')."',unix_timestamp(),unix_timestamp())";
$st=$dbAdapter->query($sql);
$params=[
   'hash'=>$nzbHash,
   'categoryid'=>$moviesHdCategoryId,
   'releaseName'=>$nzbReleaseName,
   'imdbId'=>$nzbImdbId,
   'srcLinkUri'=>$nzbLink,
   'srcTitle'=>$nzbEntryTitleRaw,
   'rssUri'=>$rssLink,
   'pubdate'=>$nzbPublishedDateUnixTimestamp,
   'description'=>$nzbEntryDescriptionRaw
];
$st->execute($params);

这是我当前的mysql架构:

DROP TABLE `nzbsources`;
CREATE TABLE `nzbsources` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentid` int(11) DEFAULT '0',
  `flags` int(5) DEFAULT '0',
  `categoryid` int(11) not null default 0,
  `hash` varchar(255) not null default '',  
  `releaseName` varchar(255) not null default '',
  `imdbId` int(11) not null default 0,
  `srcLinkUri` varchar(255) not null default '',
  `srcTitle` varchar(255) not null default '',
  `rssUri` varchar(255) not null default '',
  `pubdate` int(11) not null default 0,
  `status` int(5) NOT NULL DEFAULT '1',
  `description` text,
  `created` int(11) NOT NULL,
  `updated` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_parent` (`id`,`parentid`),
  UNIQUE KEY `hash` (`hash`)
) ENGINE=InnoDB;

DROP TABLE `nzbcategories`;
CREATE TABLE `nzbcategories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentid` int(11) DEFAULT '0',
  `flags` int(5) DEFAULT '0',
  `name` varchar(255) not null default '',
  `status` int(5) NOT NULL DEFAULT '1',
  `description` text,
  `created` int(11) NOT NULL,
  `updated` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_parent` (`id`,`parentid`)
) ENGINE=InnoDB;

1 个答案:

答案 0 :(得分:0)

看起来问题在于如何创建$ sql,似乎缺少&#39;:&#39;使绑定参数工作。

例如插入查询应如下所示,其中值需要具有以下值:&#39;在他们面前。

INSERT INTO USER (FIRST_NAME, LAST_NAME) VALUES (:FIRST_NAME, :LAST_NAME);

您的$ params值已正确添加。

$params = array(
    'FIRST_NAME' => 'John',
    'LAST_NAME' => 'Smith'
);

我希望这会有所帮助。