如何将 PHP 对象转储到 SQL Table 中?
How to dump PHP object into an SQL Table?
我想知道是否有快速的方法来执行以下操作:
我有一个具有多个属性的 PHP 对象。例如
$person;
$person->height = 165;
$person->name = 'john';
我想将其转储到 SQLite MySQL 数据库中,同时根据 PHP 对象的属性名称自动创建每一列。因此,例如,如果我将上述对象转储到 mySQL,它将看起来像这样:
Table : people
Name(Column, VARCHAR) - "John"
Height(Column, INT) - 165
我问这个问题的原因是属性的数量在不断增加,而且必须手动创建和管理 table 列是一项繁重的工作。我想知道是否有自动执行此操作的方法。
首先您需要将 object
转换为 array
然后您可以遍历它并可以在其中创建 table
和 insert
值。
如下所示:
第 1 步:转换 object to array
第 2 步:从数组中获取键(字段)和值
第 3 步:生成 sql 查询
<?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);
?>
希望这对您有所帮助(y)。
我想知道是否有快速的方法来执行以下操作:
我有一个具有多个属性的 PHP 对象。例如
$person;
$person->height = 165;
$person->name = 'john';
我想将其转储到 SQLite MySQL 数据库中,同时根据 PHP 对象的属性名称自动创建每一列。因此,例如,如果我将上述对象转储到 mySQL,它将看起来像这样:
Table : people
Name(Column, VARCHAR) - "John"
Height(Column, INT) - 165
我问这个问题的原因是属性的数量在不断增加,而且必须手动创建和管理 table 列是一项繁重的工作。我想知道是否有自动执行此操作的方法。
首先您需要将 object
转换为 array
然后您可以遍历它并可以在其中创建 table
和 insert
值。
如下所示:
第 1 步:转换 object to array
第 2 步:从数组中获取键(字段)和值
第 3 步:生成 sql 查询
<?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);
?>
希望这对您有所帮助(y)。