我遇到了一些代码问题而我正在努力实现这一目的:首先,它将提取具有相同公司ID的所有记录并从中进行类型上传一个表,然后它将结果与第二个表中的记录进行比较。通常情况下,这对我来说不是一个问题......简单。
那就是说,我的问题在于几个因素:查询来自一系列类。这是使用的基本功能:
public function query($query, $params = array())
{
try {
//$query = $this->build_query($query, $table);
$params = $this->build_params($params);
$params = $this->clean_params($query, $params);
$this->lastquery = $this->pdo->prepare($query);
//$this->lastquery->bindParam(':table', $this->DBPrefix . $table, PDO::PARAM_STR); // must always be set
foreach ($params as $val)
{
$this->lastquery->bindParam($val[0], $val[1], @$val[2]);
}
$this->lastquery->execute();
//$this->lastquery->debugDumpParams();
}
catch(PDOException $e) {
//$this->lastquery->debugDumpParams();
$this->error_handler($e->getMessage());
}
//$this->lastquery->rowCount(); // rows affected
}
现在,这是我已经获得的代码。请注意,在此之前提供了company_id,$ DBPrefix被定义为数据库的所有表上使用的前缀。哦,现在忽略了这些参数......我还没有包括那部分,但它会完成:
unlink($includeFile);
//Start Scan Procedures
//Step one - Read all records with type "uploads" & company_id that match current company_id
$query = "SELECT property_id, account_id, company_id, description, pict_url, photo_uploaded, bed,
bath, sqft, built, embed_url, ext_tour_url, street, city, state, zip, unit, agent,
lat, lng, mlsid, ptype, pets, furnished, length, security_deposit, pet_deposit,
filename, created, type, status
FROM " . $DBPrefix . "properties_uploads
WHERE company_id = " . $company_id . "
AND type = 'uploads'";
$db->query($query);
$rec_to_upload = $db->result();
$count_uploads = 0;
foreach ($rec_to_upload as $rec) {
$query = "SELECT property_id
FROM " . $DBPrefix . "properties
WHERE company_id = " . $company_id . "
AND street = '" . $rec['street'] . "'
AND city = '" . $rec['city'] . "'
AND state = '" . $rec['state'] . "'
AND unit = '" . $rec['unit'] . "'";
if ($db->numrows() == 0) {
//update to be an original type
$query = "UPDATE " . $DBPrefix . "properties_uploads
SET type = 'original'
WHERE property_id = " . $rec['property_id'];
$db->query($query);
$count_uploads++;
} else {
//update to be a duplicate type
$query = "UPDATE " . $DBPrefix . "properties_uploads
SET type = 'duplicate'
WHERE property_id = " . $rec['property_id'];
$db->query($query);
}
}
当我自己通过MYSQL运行SQL查询时,它们运行得非常好......每次都得到正确的响应。
然而,当我测试它时,$ rec_to_upload结果作为单个记录返回,而不是应该显示的9。它不会比一次运行foreach循环更多。如果我运行numrows函数,我们必须确定行数,它在第一个查询中是正确的,但它在第一次内部查询时保留其结果,然后转到0并在0和1之间跳转。属性表是空的,所以它应该一直为0。
我无法执行$ rec [' city']因为它只运行一条记录。如果我执行$ rec_to_upload [' city']会有结果,但这仍不是多条记录的解决方案。
虽然它应该改变"键入"对于原始或重复的所有记录,它会将一些记录保留为上传。
最后,第一个查询的结果似乎推动了两次:我同时获得了关联结果和数值结果。
提前感谢您提供的任何帮助。如果您有任何问题需要帮助澄清,我非常乐意回答这些问题。
其他信息。它是从第一个有问题的查询开始的。我做了一个备份,然后清除了sql之后的所有内容,直到我留下了这个:
//Start Scan Procedures
//Step one - Read all records with type "uploads" & company_id that match current company_id
$query = "SELECT property_id, account_id, company_id, description, pict_url, photo_uploaded, bed,
bath, sqft, built, embed_url, ext_tour_url, street, city, state, zip, unit, agent,
lat, lng, mlsid, ptype, pets, furnished, length, security_deposit, pet_deposit,
filename, created, type, status
FROM " . $DBPrefix . "properties_uploads
WHERE company_id = " . $company_id . "
AND type = 'uploads'";
$db->query($query);
$rec_to_upload = $db->result();
$i = 1;
foreach ($rec_to_upload as $rtu) {
echo $i . '<br>';
$i++;
}
echo $db->numrows();
结果:记录了62条记录。共有9条记录,共有30列。看起来它已经过去并且逐字段地放置一条记录,字段作为单独的记录......关联和数字,而不是输入行。
答案 0 :(得分:1)
请考虑以下事项。我指定的要求与您的要求略有不同,但我认为原则保持不变:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
,type VARCHAR(20) NOT NULL
);
INSERT INTO my_table VALUES
(1,'Adam','unreconciled'),
(2,'Ben','reconciled'),
(3,'Charlie','reconciled'),
(4,'Adam','unreconciled'),
(5,'Ben','unreconciled'),
(6,'Dan','unreconciled');
SELECT * FROM my_table;
+----+---------+--------------+
| id | name | type |
+----+---------+--------------+
| 1 | Adam | unreconciled |
| 2 | Ben | reconciled |
| 3 | Charlie | reconciled |
| 4 | Adam | unreconciled |
| 5 | Ben | unreconciled |
| 6 | Dan | unreconciled |
+----+---------+--------------+
SELECT x.*
, CASE WHEN y.id IS NOT NULL THEN 'duplicate' ELSE 'original' END type
FROM my_table x
LEFT
JOIN my_table y
ON y.name = x.name
AND y.id < x.id
WHERE x.type = 'unreconciled';
+----+------+--------------+-----------+
| id | name | type | type |
+----+------+--------------+-----------+
| 1 | Adam | unreconciled | original |
| 4 | Adam | unreconciled | duplicate |
| 5 | Ben | unreconciled | duplicate |
| 6 | Dan | unreconciled | original |
+----+------+--------------+-----------+
UPDATE my_table x
LEFT
JOIN my_table y
ON y.name = x.name
AND y.id < x.id
SET x.type = CASE WHEN y.id IS NOT NULL THEN 'duplicate' ELSE 'original' END
WHERE x.type = 'unreconciled';
Query OK, 4 rows affected (0.04 sec)
SELECT * FROM my_table;
+----+---------+------------+
| id | name | type |
+----+---------+------------+
| 1 | Adam | original |
| 2 | Ben | reconciled |
| 3 | Charlie | reconciled |
| 4 | Adam | duplicate |
| 5 | Ben | duplicate |
| 6 | Dan | original |
+----+---------+------------+
这相当于总共一个查询。不是2个查询,不是3个查询,不是每个循环1个查询。只需1个查询。如果您能够根据自己的目的进行调整,那么它将比您现有的解决方案快几个数量级。
答案 1 :(得分:0)
问题解决了!
我做了什么,考虑到它覆盖了$ db-&gt;结果是尝试了几种方法来阻止它...试图填充一个数组。我终于成功了:
while ($row = $db->result()) {
$prop_data[] = $row;
}
然后从那里开始。感谢所有试图帮助的人!