I receive the following error...
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
for the following script...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT * FROM tablename");
$stmt->execute();
while($db->fetch(PDO_FETCH_ASSOC) as $row) {
$id= $row['id'];
$name= $row['name'];
}
$db->commit();
}
catch (PDOException $e)
{
$db->rollback();
echo "There was a system error.<br>".$e->getMessage();
}
?>
Any idea what is throwing the error? I have checked for missing semicolons, commas, and the works but got nothing!
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
T_AS is the token for as in the PHP interpreter. It was unexpected when trying to parse your code's syntax.
as is only valid in a foreach loop, and you are using a while.
Change your while loop to a foreach loop.
Fatal error: Call to undefined method PDO::fetch() in index.php on line 113
This is a run time error - the PDO object has no method called fetch(). Are you calling fetch() on the right object?
Check out the documentation.
As Wrikken states in the comments, it will be a method of your $stmt object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With