简体   繁体   English

即使查询相同,PHP返回的结果也比phpmyadmin少

[英]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 ,即mysqliPDO 。)

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.

相关问题 MySQL 使用相同的查询在 PHP 和 PHPMyadmin 上返回不同的结果 - MySQL returning Different results on PHP and PHPMyadmin using same Query PHP查询返回相同的数据,即使它已更改 - PHP Query Returning the Same Data Even Though It Changes PHP返回的结果少于mysql - PHP returning less results than mysql PHP SQL查询未返回与phpMyAdmin中相同的行 - PHP SQL query not returning same rows as in phpMyAdmin PHP:PDO查询不返回任何结果,但是同一查询在phpmyadmin中返回4个结果? - PHP: PDO Query returns no results, but the same query returns 4 results in phpmyadmin? 即使没有结果返回,查询计数也将返回1 - Query count returns as 1 even though there's no results returning PHP中带有&#39;&&#39;的MySQL查询返回的结果与phpMyAdmin的结果不同 - MySQL query in PHP with '&' returns different results than phpMyAdmin does PHP mysql_query重试0个结果,即使它应为450 - PHP mysql_query returing 0 results even though it should be 450 相同的查询,php vs phpmyadmin中的结果不同-为什么? - Same query, different results in php vs phpmyadmin- Why? 为什么在“YYYY-MM-DD”的日期格式中,两个字符串的PHP中的比较小于或者多于比较,即使它们是字符串? - Why does a less than or more than comparison in PHP of two strings in the date format of “YYYY-MM-DD” work even though they are strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM