duanlu1793 2012-01-25 17:22
浏览 55
已采纳

基于MySQL列数据类型的动态PHP代码创建

I am developing a tool that creates PHP classes based on tables in my database. Most of the functionality is based on data from SHOW COLUMNS FROM $table. Each column typically relates to an instance variable in the new class being created.

What I have now is a situation where if a user passes in "twelve" for a variable related to a column of type INT, it doesn't fail, but the value in the database becomes 0.

What I would like is for the class to return false, identifying that the UPDATE or INSERT could not be completed.

What approach would you recommend for this?

What I'm trying now, is to construct an array of column names that contains each column that has a numeric data type (INT, FLOAT etc.). And when the user tries to set a variable, it checks to see if the variable is in this list and then checks the value being set to determine if it is a number.

PHP 5.3.3 using MySQLi

I'm assuming any column that isn't numeric will accept text data.

  • 写回答

4条回答 默认 最新

  • douying4203 2012-01-25 17:38
    关注

    What are you are asking is called ORM. There are some good ORM libraries in PHP.

    1. Doctrine
    2. Propel
    3. Outlet ORM
    4. Leap ORM for Kohana ( I use it )
    5. PHP Data Mapper

    BTW, I answer your question.

    If any invalid data is set you can throw an Exception.

    See a sample.

    class MyTable extends DbTable{
        function __set($var, $val){
            switch($var){
                case 'age': // age should be numeric type
                    if(!is_numeric($val)){
                        throw new InvalidDataTypeException("$val of $var is not numeric");
                    }
                 /// do other operation here
    
                 ... 
                 ... // more cases
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算