Automatically clip strings to the proper length on insert

前端 未结 3 713
你的背包
你的背包 2021-01-15 16:35

I have a table with various VARCHAR fields in MySQL. I would like to insert some user data from a form via PHP. Obviously if I know the field lengths in PHP, I can limit the

3条回答
  •  执念已碎
    2021-01-15 17:13

    You can use column metadata to check string length. (PHP Manual on PDOStatement->getColumnMeta)

    Get metadata for whole table this way

    $query = $conn->query("SELECT * FROM places");
    $numcols = $query->columnCount();
    
    $tablemeta = array();
    for ($i = 0; $i < $numcols; $i++) {
        $colmeta = $query->getColumnMeta($i);
        $tablemeta[$colmeta['name']] = $colmeta;
    }
    

提交回复
热议问题