我正在使用PHP版本7.0.32和MySQL 5.6
我创建了一个简单的查询生成器,该生成器确定如何根据给定的参数构建查询。我正在使用PDO连接到我的数据库并执行查询。查询本身很好,我将在下面提供示例。当我直接在数据库中运行查询时,会得到正确的返回结果。但是,每当我运行代码并通过PHP查询数据库时,似乎所有没有排序规则集的列值都将变为NULL。
令人困惑的部分是,这仅在一个特定查询中发生。我所有其他查询都在列中返回了正确的值,其他查询中的一个包含了工作不正常的查询的结果集,但在这种情况下却给出了正确的值。
工作查询:
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;
不起作用的查询:
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;
正确的结果集将返回接近如下的值:
[“ formID”] =>字符串(1)“ 1” [“ personnelID”] => int(0)[“ storeName”] =>字符串(6)“ Albany”
要使一个查询不正确的结果集不起作用,如下:
[“ formID”] =>字符串(1)“ 1” [“ personnelID”] => NULL [“ storeName”] =>字符串(11)“ River Ridge”
如果研究此问题,我发现当Collation和php charset不能一起工作时,可能会发生这种情况。我已经确保所有设置都相同,但是对于INT,FLOATS,DATE等类型,我无法做到这一点。
对于该主题的任何帮助以及任何解决方案,我将不胜感激。询问我是否还有其他信息可以帮助您。
答案 0 :(得分:0)
我找到了问题,原来是一个小问题,没有报告错误。
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;
上述查询的问题在于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;
将sub
引用添加到*允许数据从正确的位置提取并返回该数据。我不确定为什么没有错误,并且返回的某些数据没有问题。我想了解为什么它部分起作用,而不是失败或失败。