Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "Unexpected T_AS" in PHP PDO request?

Tags:

php

mysql

pdo

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!

like image 852
JM4 Avatar asked May 23 '26 12:05

JM4


1 Answers

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.

Update

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.

like image 171
alex Avatar answered May 25 '26 06:05

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!