最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Correct PHP to output a single result of a Function with SQL Query

programmeradmin1浏览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 ?>

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 ?>
Share Improve this question edited Oct 30, 2020 at 23:17 pinkequine asked Oct 30, 2020 at 22:55 pinkequinepinkequine 112 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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(); ?>
发布评论

评论列表(0)

  1. 暂无评论