PHP MySQL PDO: how to preserve leading zeros of zerofill int columns

后端 未结 2 1492
余生分开走
余生分开走 2021-02-13 04:56

I\'ve hit one more bump in the road of migrating from the old mysql_*() functions to the new PDO class: I have a the following table:

CREATE TABLE `         


        
2条回答
  •  生来不讨喜
    2021-02-13 05:03

    I'd write small routine to patch the PDO output to suit the requirements, and try to make the least amout of changes to the coding.

    $results = pdoFix($SqlResult->fetchAll(PDO::FETCH_BOTH))

    function pdoFix($results) {
        foreach ($results as &$row) { // note the "&"
          $row[0] = sprintf("%'04s",$row[0]); // zerofill '0'
          $row['id'] = sprintf("%'04s",$row['id']); // zerofill 'id'
        }
        unset($row); // break the reference with the last element
        return $results;
    }
    

    Note: The other answers are just as good, pick one that you are most comfortable with.

提交回复
热议问题