php mysql查询在有值的列中返回null

wdebmtf2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(477)

我使用的是php版本7.0.32和MySQL5.6
我创建了一个简单的查询生成器,它根据给定的参数确定如何构建查询。我使用pdo连接到数据库并执行查询。查询本身的结果很好,我将在下面提供示例。当我直接在数据库中运行查询时,我得到了正确的返回结果。但是,每当我运行代码并通过php查询db时,似乎所有没有排序规则集的列值都变为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”]=>string(1)“1”[“personnelid”]=>int(0)[“storename”]=>string(6)“奥尔巴尼”
为一个不工作的查询设置不正确的结果如下:
[“formid”]=>string(1)“1”[“personelid”]=>null[“storename”]=>string(11)“river ridge”
如果研究这个问题,我发现当排序规则和php字符集不能一起工作时,就会发生这种情况。我已经确保所有设置都是相同的,但是对于int、floats、date等类型我不能这样做。
如果您能在这个问题上给予我任何帮助,我将不胜感激。询问我是否有其他信息可以提供帮助。

vlju58qv

vlju58qv1#

我发现了问题,原来是个小问题,没有报错。

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 对*的引用允许从适当的位置提取数据并返回该数据。我不知道为什么没有错误,有些数据返回时没有问题。我很想知道为什么它部分有效,而不是失败或通过。

相关问题