I want to echo the results of this mysql query

SELECT username, task_completed FROM accounts WHERE referrer_by = :referrer_by AND task_completed < 1

I have this code so far but this doesnt return anything and it should return the values which are less than 1

$ref_query=$db->prepare("SELECT username, task_completed FROM accounts WHERE referrer_by = :referrer_by AND task_completed < 1");
$ref_query->bindValue(":referrer_by", $user);

$result = $ref_query->execute();

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<br> Username: " . $row["username"] . " - Tasks: " . $row["task_completed"] . " <br>";
    }
}

Recommended Answers

Sorry, I'm not familiar with the PHP framework you're using. Does bindValue() properly escape the $user variable?
Also, do you know if it's returning num_rows at all? What about echo'ing echo $result->num_rows; right above the if statement to see if it's even reaching the while loop.

Jump to Post

Firstly, add thsi to the top of your page to see any errors returned, this only whilst in development - error_reporting(E_ALL); ini_set('display_errors', 1);

As we don't know what is in your data table, is there any records to be returnd that is < 1?
What do you gte when you …

Jump to Post

All 5 Replies

Sorry, I'm not familiar with the PHP framework you're using. Does bindValue() properly escape the $user variable?
Also, do you know if it's returning num_rows at all? What about echo'ing echo $result->num_rows; right above the if statement to see if it's even reaching the while loop.

@Dani thank you for your reply. I did tried with echoing the num_rows but didnt return anythng also.

Firstly, add thsi to the top of your page to see any errors returned, this only whilst in development - error_reporting(E_ALL); ini_set('display_errors', 1);

As we don't know what is in your data table, is there any records to be returnd that is < 1?
What do you gte when you echo out?

For some or other reason I cannot edit my previous code, wanted to add the following code -

Dani, pse check, I added first comment, wanted to add code but from there, code block shows with all my code attempts but does not want to post the code??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.