简体   繁体   English

PHP MYSQL查询在带有值的列中返回null

[英]PHP MYSQL Query returning null in columns with values

I'm using PHP Version 7.0.32 and MySQL 5.6 我正在使用PHP版本7.0.32和MySQL 5.6

I created a simple query generator that determine how to build a query based on the parameters given. 我创建了一个简单的查询生成器,该生成器确定如何根据给定的参数构建查询。 I'm using PDO to connect to my DB and execute queries. 我正在使用PDO连接到我的数据库并执行查询。 The queries themselves are coming out fine, and I will provide samples below. 查询本身很好,我将在下面提供示例。 When I run the queries in the database directly I get the proper returned results. 当我直接在数据库中运行查询时,会得到正确的返回结果。 However, whenever I run the code and query the DB through PHP, it seems like all the column values that do not have a Collation set become NULL. 但是,每当我运行代码并通过PHP查询数据库时,似乎所有没有排序规则集的列值都将变为NULL。

The confusing part is that this only happens with one specific query. 令人困惑的是,这仅在一个特定查询中发生。 All of my other queries return the proper values in the columns, and one of the other queries includes the result set of the improperly working query but is properly giving the values in this case. 我所有其他查询都在列中返回了正确的值,而其他查询中的一个包含了工作不正常的查询的结果集,但在这种情况下却给出了正确的值。

Working Queries: 工作查询:

SELECT * FROM fm_submission AS sub WHERE sub.archived = :archived ORDER BY sub.dateSubmitted LIMIT :start, :limit;

SELECT * FROM fm_submission AS sub LEFT JOIN fm_submission_saved AS saved ON sub.submissionID = saved.submissionID WHERE sub.archived = :archived AND saved.submissionID IS NOT NULL ORDER BY sub.dateSubmitted LIMIT :start, :limit;

SELECT * FROM fm_submission AS sub WHERE sub.archived = :archived ORDER BY sub.dateSubmitted;

Not Working Query: 不起作用的查询:

SELECT * FROM fm_submission AS sub LEFT JOIN fm_submission_saved AS saved ON sub.submissionID = saved.submissionID WHERE sub.archived = :archived AND saved.submissionID IS NULL ORDER BY sub.dateSubmitted LIMIT :start, :limit;

A proper result set returns close to as follows: 正确的结果集将返回接近如下的结果:

["formID"]=> string(1) "1" ["personnelID"]=> int(0) ["storeName"]=> string(6) "Albany" [“ formID”] =>字符串(1)“ 1” [“ personnelID”] => int(0)[“ storeName”] =>字符串(6)“ Albany”

To improper result set for the one query not working is as follows: 要使一个查询不正确的结果集不起作用,如下所示:

["formID"]=> string(1) "1" ["personnelID"]=> NULL ["storeName"]=> string(11) "River Ridge" [“ formID”] =>字符串(1)“ 1” [“ personnelID”] => NULL [“ storeName”] =>字符串(11)“ River Ridge”

If researching the issue I found that this can happen when there is an issue with the Collation and php charset not working together. 如果研究此问题,我发现当Collat​​ion和php charset不能一起工作时,可能会发生这种情况。 I have made sure that all are set to be the same, however I can't do that for types like INT, FLOATS, DATE, etc. 我已经确保所有设置都相同,但是对于INT,FLOATS,DATE等类型,我无法做到这一点。

I would appreciate in any help on the topic and any solutions even more. 如果您对该主题有任何帮助,请提供任何解决方案,我们将不胜感激。 Ask if there is any other information I can provide to help. 询问是否还有其他我可以提供帮助的信息。

I found the problem, which turned out to be a small problem that didn't report an error. 我找到了问题,原来是一个小问题,没有报告错误。

SELECT * FROM fm_submission AS sub LEFT JOIN fm_submission_saved AS saved ON sub.submissionID = saved.submissionID WHERE sub.archived = :archived AND saved.submissionID IS NULL ORDER BY sub.dateSubmitted LIMIT :start, :limit;

The issue with the above query was the ambiguous nature of the * in the SELECT. 上面查询的问题是SELECT中*的模棱两可性质。

SELECT sub.* FROM fm_submission AS sub LEFT JOIN fm_submission_saved AS saved ON sub.submissionID = saved.submissionID WHERE sub.archived = :archived AND saved.submissionID IS NULL ORDER BY sub.dateSubmitted LIMIT :start, :limit;

Adding the sub reference to the * allowed the data to pull from the proper place and return that data. sub引用添加到*允许数据从正确的位置提取并返回该数据。 I'm not sure as to why there wasn't an error and some of the data was returned without issue. 我不确定为什么没有错误,并且返回的某些数据没有问题。 I'd be interested in learning why it partially worked, instead of failing or passing. 我会对了解为什么它部分起作用而不是失败或失败感兴趣。

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

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