0

The name of the table is "hit" and the name of the row that i would like the sum of is "amount" but my code is not working

 <?php 
        $result = $mysqli->query('SELECT sum(amount) FROM hits');
        if (FALSE === $result) die("Select sum failed: ".mysqli_error);
        $row = mysqli_fetch_row($result);
        $sum = $row[0];
        print $sum; 

        ?>

The error i get back is "Notice: Use of undefined constant mysqli_error - assumed 'mysqli_error' in ..."

2 Answers 2

1
if (FALSE === $result) die("Select sum failed: ".mysqli_error);

should be

if (FALSE === $result) die("Select sum failed: ".$mysqli->error());
Sign up to request clarification or add additional context in comments.

2 Comments

ahh thank you John Conde. my mistakes are alwaaays obvious and then i feel guilty about making a whole post for it lol! thanks again
@John Conde, I don't see mysqli_error() method exists. Can you paste the link?
0

There is no such mysqli_error() method available in PHP till now. There is mysql_error() you shouln't confuse with it.

The solution is

if (FALSE === $result) die("Select sum failed: " . $mysqli->error);

More MySQLi Error

3 Comments

Thank you. i did both of your ways and got the same error "Select sum failed: Table 'hits.hits' doesn't exist" i dont have "hits.hits" typed anywhere in my code so im stuck.
What is your database name? Do you configured correctly? $mysqli = mysqli_connect('hostname', 'username', 'password', 'dbname');?
i mixed them up. my database's name is "hits" and the table's name is "hit"... i had the database's name instead of the table's name..! edit: also, thanks for the link.

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.