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

I get 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 throws an error? I checked the missing semicolons, commas and works, but received nothing!

+3
source share
2 answers

Parse error: syntax error, unexpected T_AS in .... \ index.php on line 98

T_ASis the token for asin the PHP interpreter. This was unexpected when trying to parse your code syntax.

asonly acts in a loop foreachand you use while.

Change cycle whileto cycle foreach.

Update

Fatal error: call to undefined PDO :: fetch () method in index.php on line 113

. PDO fetch(). fetch() ?

.

Wrikken , $stmt.

+7

"" "while", . "while" "foreach", .

+6

Source: https://habr.com/ru/post/1773165/


All Articles