2

New to PDO ... i am trying to get sum but getting error here is my code

function test_score_month($student_id){
    global $host, $dbname, $user, $pass;

    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $STH = $DBH->query("SELECT SUM(score), SUM(score_from) FROM school_test_report
    WHERE 
     school_test_report.test_date >= last_march() and school_test_report.test_date <= march()" );
    $STH->setFetchMode(PDO::FETCH_BOTH);
    return $STH;
}

out put is

$student_id = test_score_month($name);
echo $student_id['SUM(score)'];

but getting error

Call to a member function setFetchMode() on a non-object in

i have tryed

$STH->setFetchMode(PDO::FETCH_ASSOC);

but still same error

3
  • 1
    Does $STH have error info? nl3.php.net/manual/en/pdostatement.errorinfo.php Commented May 27, 2012 at 11:09
  • This is why PDO should be configured to throw exceptions on errors. Your query fails to be executed (because of WHERE AND), but you don't check the return value. Commented May 27, 2012 at 11:11
  • sorry ad was typing mistake still getting that error without AND Commented May 27, 2012 at 11:14

1 Answer 1

1

You have syntax error in your query:

WHERE 
    and

also are you sure that you have march() and last_march() functions in your DB ?

[+] Always check for errors after querying database:

if($STH){
 // do your stuff
}else{
 die($DBH->errorInfo()); // see the error
}

See http://www.php.net/manual/en/pdo.errorinfo.php

Sign up to request clarification or add additional context in comments.

3 Comments

sorry and was typing mistake still getting that error without AND
now i am trying to modify my script and still getting error SELECT SUM(score), SUM(score_from) FROM school_test_report WHERE student_id='$student_id' keeping it simple now i am getting output error $student_id = test_score_month($name); echo $student_id['SUM(score)']; Cannot use object of type PDOStatement as array
@Harinder: Are you doing $STH->fetch()?

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.