0

I was trying to make a weighted avg. value for my 5 star rating script:

function raten($setvl)
{
    global $book_id, $resulten, $dbhandle;
    $resulten =  mysql_query("SELECT SUM(rate) 
                                FROM review 
                               WHERE rate = '$setvl' AND 
                                     book_id = '$book_id",
                            $dbhandle);
    $rowen = mysql_fetch_array($resulten);
    return $rowen['SUM(rate)'];
}

Can anyone explain what I'm doing wrong?

4
  • how about making SELECT SUM(rate) as tot_rate in the query and then return $rowen['tot_rate']; Commented Mar 9, 2014 at 15:48
  • It's impossible to say where the issue is if you don't provide more details. What's $resulten? Is the query actually returning any row (if you run it in the console)? Is the connection actually working? What does $rowen contain (is $rowen['SUM(rate)'] actually returned by the query)? Commented Mar 9, 2014 at 15:49
  • Try if(!mysql_query(...)) /*error*/ else{ if(!mysql_fetch_array(...)) /*error*/ { /*handle result*/} and see what happens Commented Mar 9, 2014 at 15:49
  • 3
    PS: your coding style is really PHP 4-ish. If you're following a book, I'd recommend you to update it. The use of mysql_* functions is not recommended, and using global parameters is not really the best practice... Commented Mar 9, 2014 at 15:49

1 Answer 1

1

You have missed a ' in your query. Change...

"SELECT SUM(rate) FROM review WHERE rate = '$setvl' AND book_id = '$book_id"

to...

"SELECT SUM(rate) FROM review WHERE rate = '$setvl' AND book_id = '$book_id'"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.