duanjiao6731 2016-12-12 05:58
浏览 52
已采纳

如何将PHP对象转储到SQL表中?

I'm wondering if there is a quick way to do the following:

I have a PHP object with multiple attributes. For example

$person;
$person->height = 165;
$person->name = 'john';

I would like to dump this into an SQLite MySQL DB while creating each column automatically from the attribute names of the PHP object. So for example, if I were to dump the above object into mySQL, it would look something like this:

Table : people
Name(Column, VARCHAR)  -  "John"
Height(Column, INT)  -  165 

The reason I am asking this is because the number of attributes is growing constantly, and having to create and manage the table columns manually is a lot of work. I am wondering if there is an automated way of doing this.

  • 写回答

1条回答 默认 最新

  • duanqiongchong0354 2016-12-12 08:15
    关注

    First you need to convert object into array and then you can iterate through it and can create table and insert values in it.

    Something like below:

    Step 1: Convert object to array

    Step 2: Get keys(fields) and values out of array

    Step 3: Generate sql queries

        <?php
        //Step 1: convert object to array
        //$persion =  (array) $yourObject;
    
        //Step 2: get keys(fields) and values out of array
        $person = array(
            "height" => "165",
            "name" => "john",
            "age" => "23"
        );
    
        function data_type($val) {
            if(is_numeric($val)) { 
                return "int"; 
            } 
            else {
                return "varchar(15)";
            }   
        }
    
        //Step 3: sql query, only including sql query
        function create_table($person) {
            $create = "CREATE TABLE IF NOT EXISTS people";
            $ctr = 0;
            foreach($person as $key => $value) {
                if($ctr == 0) {
                    $field_query = $key." ".data_type($value);
                } else {
                    $field_query .= ", ".$key." ".data_type($value);
                }
                $ctr++;
            }
            echo $create .= " (".$field_query.")";
            echo "<br/>";
        }
        create_table($person);
    
        function insert_table($person) {
            $ctr = 0;
            foreach($person as $key => $value) {
                if($ctr == 0) {
                    $field_query = $key;
                    $value_query = $value;
                } else {
                    $field_query .= ", ".$key;
                    $value_query .= ", ".$value;
                }
                $ctr++;
            }
            echo $insert = "INSERT INTO people"." (".$field_query.") VALUES (".$value_query.")";
        }
        insert_table($person);
    
        ?>
    

    Hope this will help you in some way(y).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀