How to use PDO to fetch results array in PHP?

前端 未结 3 988
心在旅途
心在旅途 2020-11-22 10:09

I\'m just editing my search script after reading up on SQL injection attacks. I\'m trying to get the same functionality out of my script using PDO instead of a regular mysql

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 10:34

    Take a look at the PDOStatement.fetchAll method. You could also use fetch in an iterator pattern.

    Code sample for fetchAll, from the PHP documentation:

    prepare("SELECT name, colour FROM fruit");
    $sth->execute();
    
    /* Fetch all of the remaining rows in the result set */
    print("Fetch all of the remaining rows in the result set:\n");
    $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
    print_r($result);
    

    Results:

    Array
    (
        [0] => Array
            (
                [NAME] => pear
                [COLOUR] => green
            )
    
        [1] => Array
            (
                [NAME] => watermelon
                [COLOUR] => pink
            )
    )
    

提交回复
热议问题