简体   繁体   English

PHP查询未返回任何结果

[英]PHP query returns no results

I'm probably missing something obvious but when I try to execute this query, it returns no results. 我可能缺少明显的东西,但是当我尝试执行此查询时,它不返回任何结果。 I plugged it directly into MySQL and also tried replacing the variable with a valid row value and I get the correct output. 我直接将其插入MySQL,还尝试用有效的行值替换变量,然后得到正确的输出。 When I use a variable, it gives me no results. 当我使用变量时,没有任何结果。 Anyone have any thoughs? 有人有事吗?

    $query = "SELECT title FROM le7dm_pf_tasks WHERE project = (SELECT id FROM le7dm_pf_projects WHERE title = '".$ws_title."') ORDER BY title DESC LIMIT 1";

    $result_query = mysql_query($query) or die("Error: ".mysql_error());

    while ($row = mysql_fetch_assoc($result_query)) {
        $result_title = $row['title'];
    }

    $result_title = substr($result_title,0,6);
    echo $result_title;

Your SQL could do with some rework (though not the reason for your issue). 您的SQL可以做一些重做(尽管不是您问题的原因)。 No need for the nested select (which can also cause an error if it returns > 1 row). 不需要嵌套选择(如果返回> 1行,这也会导致错误)。 Try a join. 尝试加入。

$sql = "
    SELECT title FROM le7dm_pf_tasks t
        INNER JOIN le7dm_pf_projects p ON t.project = p.id
    WHERE p.title = '{$ws_title}'
    ORDER BY title DESC LIMIT 1
";

You are also iterating over an unknown number of rows using the while statement. 您还使用while语句遍历了未知数量的行。 And then you exit and attempt a substring. 然后退出并尝试创建子字符串。 How do you know that the last row iterated in the while had a value. 您如何知道在while中迭代的最后一行具有值。

Try outputting $result_title inside the while loop itself to confirm data. 尝试在while循环本身中输出$ result_title以确认数据。

echo $result_title;

If you truly only have a single row, there is no need for the while loop. 如果确实只有一行,则不需要while循环。 Just do 做就是了

$row = mysql_fetch_assoc($result_query);

strip_tags($ws_title); strip_tags($ ws_title); - is what did it! -是什么! The title was wrapped in an anchor tag that linked to that particular project page. 标题包装在链接到该特定项目页面的锚标记中。

Thanks for all the good suggestions though. 感谢所有的好的建议。 I'm gonna use some of them in the future when bug testing. 我将来会在错误测试时使用其中的一些。

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

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