简体   繁体   English

php mysqli fetch_assoc忽略左连接的空值

[英]php mysqli fetch_assoc ignores null values for left join

I have a problem and don't know why my mysqli query does not work correctly in PHP. 我有一个问题,不知道为什么我的mysqli查询无法在PHP中正常工作。

I have two tables: 我有两个表:

table 1 "reviews"
id | dateCreated | reviewText | ...

table 2 "issues"
id | reviews_id | issueText | ...

Table 1 contains reviews from users. 表1包含用户的评论。 Table 2 contains issues (complaints) for certain reviews. 表2包含某些评论的问题(投诉)。 However one review does not necesarrily need to have a issue (if nobody reported it). 但是,一项审核并不一定需要解决问题(如果没有人举报)。 The query is: 查询是:

SELECT reviews.*, issues.* 
FROM reviews 
LEFT JOIN issues 
    ON reviews.id=issues.reviews_id 
WHERE (reviews.idVenue='6438' OR reviews.idVenue='6444' OR reviews.idVenue='7590' ) 
ORDER BY 'reviews.updated' DESC 

This works perfectly if one issue exists for a review (ON clause is true). 如果存在一个需要审查的问题(ON子句为true),则此方法非常有效。

However if there is no issue existing for the review (the ON clause is not true), then reviews.id and issues.reviews_id are missing in the $row that was fetched with 但是,如果对于复审不存在任何问题(ON子句不为true),则在使用来获取的$ row中缺少reviews.id和issue.reviews_id。

if($result = $this -> mysqli -> query($qstr)){
    while($row = $result->fetch_assoc()){
        $temp[] = $row;
        }
}

Also all the other colums from issues are missing in $row because they are NULL. 同样,所有其他来自问题的列都在$ row中丢失,因为它们为NULL。 With missing I mean, that they are not there if I print $row or check $row as a watch variable in the netbeans debugger. 缺少我的意思是,如果我在netbeans调试器中打印$ row或将$ row作为watch变量检查,它们将不存在。 It just doesn't show reviews.id and issues.reviews_id. 它只是不显示reviews.id和issue.reviews_id。

If I run the query above directly in phpmyadmin, then it returns all columns correctly with the value NULL for the cases where no related issue was found for a review. 如果我直接在phpmyadmin中运行上面的查询,那么对于没有找到相关问题进行审阅的情况,它会正确返回所有列,值为NULL。

So my question is, why does PHP ignore the NULL values and does not return them in $row but phpmyadmin does? 所以我的问题是,为什么PHP会忽略NULL值,而不会在$ row中返回它们,而phpmyadmin会呢?

Thanks! 谢谢!

Here's my comment in more descriptive information in case someone runs into this in the future. 这是我对更多描述性信息的评论,以防将来有人遇到此情况。

When you call fetch_assoc php assign columns names as keys & their values as the values of the array, As the keys of an array are unique any entry that holds the same id will be overwritten. 当您调用fetch_assoc php时,将列名指定为键,将其值指定为数组的值,因为数组的键是唯一的,所以具有相同ID的任何条目都将被覆盖。

In your example both reviews & issues have a column named 'id' therefore you'll always get the value from the last 'id' column you used in your select statement. 在您的示例中,两个评论和问题都有一个名为“ id”的列,因此,您将始终从select语句中使用的最后一个“ id”列中获取值。

An easy fix for this (without having to permanently change your columns names) is to use aliases in your query. 一个简单的解决方法(不必永久更改列名)是在查询中使用别名。 eg: SELECT reviews.*, issues.id as `issue_id`, issues.issueText 例如: SELECT reviews.*, issues.id as `issue_id`, issues.issueText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM