Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
571 views
in Plugins by

I was trying to show the Tag tile (using tag description plugin), instead of a regular form of tag.

Example:

If I navigate https://site.com/tag/alibaba-and-forty-bandits, everything is dislayed correctly.

But If I navigate a random tag, or a tag without modified title (example: https://site.com/tag/alibaba-and-forty-milfs) , I get the error "Reading one value from empty results".

$resultz= qa_db_query_sub(
        'SELECT content FROM ^tagmetas WHERE title="title" AND tag=#',
        qa_html($tag)
    );
if (!empty($resultz)) {
$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_db_read_one_value($resultz));

}
else
{$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_html($tag));}

by
What's wrong with my code then?

1 Answer

+2 votes
by
edited by
 
Best answer

Hello teddydoors,

Function qa_db_query_sub can return either TRUE or a mysqli_result object; therefore, the following conditional statement

if (!empty($resultz)) {

is always true. This error can be fixed by rewritting the code like this:

$resultz = qa_db_read_one_value(qa_db_query_sub(
        'SELECT content FROM ^tagmetas WHERE title="title" AND tag=$',
        qa_html($tag)
    ), true);
if (!empty($resultz)) {
$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', $resultz);
}
else
{$qa_content['title'] = qa_lang_html_sub('main/questions_tagged_x', qa_html($tag));}

Here's what the documentation says about function qa_db_read_one_value (emphasis mine):

qa_db_read_one_value($result, $allowempty) returns the first column of the first row in the PHP $result resource. Set $allowempty to true if an empty result should not cause a fatal Q2A error.

I hope it works for you.

by
Your solution works. You're a god, even though there's a tiny typo with extra ")" sign in line 6. Thanks.
by
edited by
> there's a tiny typo with extra ")"
Fixed. Thank you for the feedback; I appreciate it.

My pleasure! Please stick with the community.
...