【发布时间】:2023-01-08 20:48:11
【问题描述】:
我有一个数据库,其中包含大约 10 列。每列应该从单独的 json 文件中获取值。我怎么能同时做到这一点?
例如 :
column1 ------ file1.json
column2 ------ file2.json
column3 ------ file3.json
column4 ------ file4.json
我的 PHP 代码是这样的:
<?php
//connect to mysql db
$con = mysql_connect("username","password","") or die('Could not connect: ' . mysql_error());
//connect to the employee database
mysql_select_db("employee", $con);
//read the json file contents
$jsondata = file_get_contents('file1.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
//get the employee details
$firstcolumn = $data['sample-column'];
//insert into mysql table
$sql = "INSERT INTO tbl_emp(sample-column, )
VALUES('$id', ')";
if(!mysql_query($sql,$con))
{
die('Error : ' . mysql_error());
}
?>
我没有任何结果
【问题讨论】:
-
停止使用过时/删除的
mysql_*API 使用mysqli_*或PDO。如果mysqli_*forks 你,看起来你也使用了一个非常旧的 php 版本。你应该升级它 -
你问的是非常基本的sql。
insert into (col1,col2, ...) values (value1, value2..)