[英]PHP returning less results than phpmyadmin even though the query is the same
When I run the following query in phpmyadmin, I get back 18 results - all of which are correct and what I'm looking for. 当我在phpmyadmin中运行以下查询时,我得到18个结果-所有这些都是正确的以及我正在寻找的东西。 However, when I copy & paste the query into a php file and run the page, I get back 17 results.
但是,当我将查询复制并粘贴到php文件中并运行该页面时,我得到了17个结果。
SELECT
ta.jobTaskID,
jt.customTaskTitle, jt.taskID, jt.status,
d.dealershipName,
j.jobNumber, j.jobID, j.title, j.jobSpec,
wt.taskName,
po.dueToProduction
FROM taskassignments ta
INNER JOIN jobTasks jt ON ta.jobTaskID = jt.jobTaskID
INNER JOIN jobs j ON jt.jobID = j.jobID
INNER JOIN dealerships d ON j.dealershipID = d.dealershipID
LEFT JOIN workflowtasks wt ON jt.taskID = wt.taskID
LEFT JOIN purchaseorders po ON j.jobID = po.jobID
WHERE ta.userID = 1 AND jt.status != 'Completed';
EDIT: Here's a snapshot of my phpmyadmin result and here's a snapshot of my var_dump . 编辑: 这是我的phpmyadmin结果 的快照,这是我的var_dump的快照 。
Here's my PHP code (I use a DB class I created) 这是我的PHP代码(我使用创建的DB类)
$myTasks = $connection->runQuery("
SELECT
ta.jobTaskID,
jt.customTaskTitle, jt.taskID, jt.status,
d.dealershipName,
j.jobNumber, j.jobID, j.title, j.jobSpec,
wt.taskName,
po.dueToProduction
FROM taskassignments ta
INNER JOIN jobTasks jt ON ta.jobTaskID = jt.jobTaskID
INNER JOIN jobs j ON jt.jobID = j.jobID
INNER JOIN dealerships d ON j.dealershipID = d.dealershipID
LEFT JOIN workflowtasks wt ON jt.taskID = wt.taskID
LEFT JOIN purchaseorders po ON j.jobID = po.jobID
WHERE ta.userID = " . $userID . " AND jt.status != 'Completed'");
Finally, here's the code the connection class uses to run a query: 最后,这是连接类用于运行查询的代码:
// runs the user defined query
public function runQuery($runMe)
{
$outArray = array();
if ($this->checkConnection()) // if the connection is a resource
{
$returned = mysql_query($runMe, $this->dbConnection);
if ($returned === false) // if there was an error during sql execution
{
echo "SQL Query error: " . mysql_error();
}
if ($returned === true) // if a update, insert, delete, etc... command was run
return true;
if (is_resource($returned))
{
$outArray = array(); // returned array with query results
for ($i = 0; $i < mysql_num_rows($returned); $i++)
{
$outArray[] = mysql_fetch_assoc($returned);
}
}
if (count($outArray) < 1)
return null;
else
return $outArray;
}
else
echo "Your Database Connection Was Unable To Be Authenticated.";
}
A better solution is not to use mysql_num_rows()
at all , but simply to keep calling mysql_fetch_assoc()
repeatedly until it stops returning results: 更好的解决方案是不使用
mysql_num_rows()
所有,而只是不断的打电话mysql_fetch_assoc()
直到它停止返回结果:
$outArray = array();
while ( $row = mysql_fetch_assoc($returned) ) {
$outArray[] = $row;
}
(Ps. Just so you know, the original PHP mysql API , which your code is using, is deprecated as of PHP 5.5.0 and will be removed in some future version. You really should move to one of the supported APIs , either mysqli or PDO .) (请注意。您知道,您的代码正在使用的原始PHP mysql API从PHP 5.5.0开始不推荐使用,并将在将来的某些版本中删除。您确实应该使用一种受支持的API ,即mysqli或PDO 。)
Try replacing this line: 尝试替换此行:
for ($i = 0; $i < mysql_num_rows($returned); $i++)
With: 附:
for ($i = 0; $i < mysql_num_rows($returned) + 1; $i++)
Your last row is being skipped because you are only checking to see if $i
is less than mysql_num_rows($returned)
. 由于仅检查
$i
是否小于mysql_num_rows($returned)
因此跳过了最后一行。 Where you should be checking to see if it is less than or equal to. 您应该在哪里查看它是否小于或等于。
So you will need to change the following line: 因此,您需要更改以下行:
for ($i = 0; $i < mysql_num_rows($returned); $i++)
to 至
for ($i = 0; $i <= mysql_num_rows($returned); $i++)
And then you will get all your data returned to your PHP as you would expect. 然后,您将按照预期将所有数据返回到PHP。
Try to clear all array used inside the loop by calling unset function after the loop to clear all array like 尝试清除循环中使用的所有数组,方法是在循环后调用unset函数以清除所有数组,例如
while(){
//your code
}
unset(array);
This will solve your problem as it solved mine. 这将解决您的问题,因为它解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.