I got the foreach() block to work, but cannot get the prepare() segment to work. Maybe that’s a signal for what I’m doing wrong. Maybe not…
[php]<?php
include(‘session.php’);
//echo $fName . " " . $lName;
$stmt = $connection->prepare('SELECT First_Name, Last_Name, SUM(Points) FROM tx_records WHERE First_Name=? AND Last_Name=?');
$stmt->execute(array($fName, $lName));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$Points = $row['SUM(Points)'];
// THIS WORKS DO NOT CHANGE FOR NOW
//foreach($connection->query("SELECT First_Name,Last_Name, SUM(Points) FROM tx_records WHERE First_Name='Allen' AND Last_Name='Troxell'") as $row){
// echo $row['First_Name'] . " " . $row['Last_Name'] . " " . $row['SUM(Points)'] . '<br>';
// }
?>[/php]
With the prepare() block I get an error of
‘Warning: mysqli_stmt::execute() expects exactly 0 parameters, 1 given in E:\bulldog\php\student_profile.php on line 7’
followed by
‘Uncaught Error: Call to undefined method mysqli_stmt::fetchAll() in E:\bulldog\php\student_profile.php:8 Stack trace: #0 {main} thrown in E:\bulldog\php\student_profile.php on line 8’.
That’s not really a mysqli statement-why does it think it is? And so I’m guessing that I am not allowed to pass an array in the parentheses? Even though I have seen it used exactly as I have it as an example in multiple sites, including at php.net, where it shows:
[php]Example #3 Execute a prepared statement with an array of insert values (placeholders)
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?> [/php]
This is nearly identical to what I am attempting. And it does not work if I simply attempt to pass the variables individually, which I have also seen in examples.
I recall seeing the '$connection->query(…") method on line 12 is frowned upon for some reason, but it is the one that is working–without variables, which defeats its purpose. I have tried to modify that segment to use variables, but I have not been successful.
And I know that $fName and $lName are valid values because I see them echoed in my web page at the top (where I’ve remmed them in this display) correctly. They arrive via the session variable.
I have now looked at numerous forums and php sites and it’s all getting a bit jumbled. What am I missing? It must be something obvious…