0

So I have created a function on a separate php inc page and want to output the single result of the function on another page.

This is the function

function get_all_rating_criteria_travel5(){
    
    global $wpdb; 
    $query = "SELECT RatingCriteriaText from ratingcriteria WHERE NicheID=1 AND StarRatingID=5";
    $result = $wpdb->get_var($query);
}

On the page I want the result I put this

<?php echo get_all_rating_criteria_travel5($result->RatingCriteriaText); ?>

I get no errors, but no result. I have tested the SELECT Query in PHPMyadmin and that produces exactly what I want. Can anyone help, I've spent hours combining different variations and I cant get it to output the database result field. I'm sure its something stupid.

EDIT: I can do this on the page and it works, but how do I do this as a function

<?php 
global $wpdb;
$rating5 = $wpdb->get_var( 'SELECT RatingCriteriaText from ratingcriteria WHERE NicheID=1 AND StarRatingID=5;')
?>
<?php echo $rating5 ?>
1
  • 1
    There does not appear to be any error handling in your code, but the most important problem is that your function does not return a value. This isn't an SQL issue, it's a basic beginner PHP problem
    – Tom J Nowell
    Commented Oct 31, 2020 at 0:59

1 Answer 1

1

Solved it! Amazing what a good nights sleep does!

Function:

function get_all_rating_criteria_travel6(){
global $wpdb;
$rating6 = $wpdb->get_var( 'SELECT RatingCriteriaText from ratingcriteria WHERE NicheID=1 AND StarRatingID=5;');
echo $rating6;
}

Code to display:

<?php get_all_rating_criteria_travel6(); ?>

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.