0

I hope you have a good day.
I get a query from the database,
If the query is empty, the message "Register" will be displayed Otherwise, the message "Register" will be displayed
See the code below and please guide me.
Nothing is displayed when the query is empty

function book_user(){
    global $wpdb;
    $rows = $wpdb->get_results("SELECT `quy` FROM wp_course_management WHERE user.current_user = '$booked_username' AND `quy` > 0");
    foreach($rows as $row){
      if (empty($row->quy)){
        echo "register.";
            }
      else{
        echo "You have alredy registered.";
        };}?>

2 Answers 2

0

Your query includes a test for quy to be non-zero, so you will never run into the situation where empty($row->quy) will be true. Furthermore, you should be testing if $rows is set or is null since you indicated that "nothing is displayed when the query is empty."

You will need to rewrite your code to test the result of the query to test for the null condition. If it is null, then the user is registered. Otherwise, they are not registered.

0

Nothing is displayed because echo "register." is never reached, which is because foreach is never run due to there being nothing in $rows to iterate.

$rows = $wpdb->get_results("SELECT `quy` FROM wp_course_management WHERE user.current_user = '$booked_username' AND `quy` > 0");
if (!$rows){
    echo "Register.";
}else{
    echo "You have already registered.";
}

or

$rows = $wpdb->get_results("SELECT `quy` FROM wp_course_management WHERE user.current_user = '$booked_username' AND `quy` > 0");
echo $rows? "You have already registered." : "Register.";
2
  • Thank you both for your guidance. Commented Oct 17, 2022 at 6:41
  • @FardadFarhang if this answer solves it please mark it as correct
    – popnoodles
    Commented Oct 19, 2022 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.