<h2>User Profile: <?php if($sth->rowCount() > 0) {$row = $sth->fetch(PDO::FETCH_ASSOC);echo " {$row['uname']}";} else {echo "No data.";}?></h2>
<span class="two">Registered Date: <?php if($sth->rowCount() > 0) {$row = $sth->fetch(PDO::FETCH_ASSOC);echo " {$row['regDate']}";} else {echo "No data.";}?> </span><br />
<span class="three">Current Grade: <?php if($sth->rowCount() > 0) {$row = $sth->fetch(PDO::FETCH_ASSOC);echo " {$row['currGrade']}";} else {echo "No data.";}unset($sth);?> </span><br />
Error I get: Fatal error: Call to a member function rowCount() on line 30
Line 30 is:
<span class="three">Current Grade: <?php if($sth->rowCount() > 0) {$row = $sth->fetch(PDO::FETCH_ASSOC);echo " {$row['currGrade']}";} else {echo "No data.";}unset($sth);?> </span><br />
SQL behind the scenes:
#Get Student Info :: for Page Content
$pdo = new PDO('mysql:host=localhost;dbname=ureviewdu', $u, $p);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
SELECT uname, currGrade, regDate
FROM Student
WHERE usrID = ?;
;');
$sth->execute(array(
$userID
));
This updated code yields nothing for each query… why?
You’re unsetting the
PDOStatement$sthvariable at the end of each block. You won’t be able to use it after unsetting it.You should probably just create one logical branch based on the result of the fetch, eg