Pdo executes the fetch query statement, and there is a 500th error. How should I write it?

<?php

try {

    $dbh = new PDO( "mysql:host=47.92.xxx.xxx;dbname=lytest","root","xxxxxxx" );

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//

    echo 1;
    $sql = "SELECT * FROM Persons";
    echo 2;
    $sth = $dbh->prepare( $sql );

    $ret = $sth->execute();
    echo 3;
    $arr1 = $ret->fetchAll(PDO::FETCH_ASSOC);
    echo 4;
    print_r($arr1);
    //echo $ret;

    $dbh = null;

} catch (PDOException $e) {
    echo $e->getMessage();
}catch( Exception $e ){
    echo $e->getMessage();
    //die($e->getMessage());
}

every time you run, you can print out "123", which proves to be
$arr1 = $ret- > fetchAll (PDO::FETCH_ASSOC);
this sentence has an error

I have tried

$arr1 = $ret->fetchAll(PDO::FETCH_ASSOC);
$arr1 = $ret->fetch(PDO::FETCH_ASSOC);
$arr1 = $ret->fetch();

is of no use. I found that there is no difference after looking for some information. How should I write it?

Nov.30,2021

//
    $sql = "SELECT * FROM Persons";
    $ret = $dbh->query($sql);
    $arr1 = $ret->fetchAll(PDO::FETCH_ASSOC);
    print_r($arr1);
  • PDO:There is no active transaction

    error The sentry background sees the error There is no active transaction , which occurs in a update method, roughly function update() { $this->db->beginTrans(); try { dosomething(); $this->db->commitTrans(); ...

    Jan.10,2022
Menu