将数组转换成MySQL数据库和PHP [英] Insert array into MySQL database with PHP

查看:362
本文介绍了将数组转换成MySQL数据库和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列我想在我的数据库来存储...

  $ insData =阵列(
    为'uid => $ fbme ['身份证'],
    'FIRST_NAME'=> $ fbme ['FIRST_NAME'],
    '姓氏'=> $ fbme ['姓氏'],
    电子邮件=>使用isset($ fbme ['邮件'])? $ fbme [电子邮件]:'',
    '链接'=> $ fbme ['链接'],
    '隶属关系'=> $网络,
    '生日'= GT; $信息[0] ['birthday_date'],
    CURRENT_LOCATION'=>使用isset($ fbme ['位置'] ['名'])? $ fbme ['位置'] ['名']:'',
    education_history'=> $教育,
    '工作'= GT; $ workInfo,
    hometown_location'=>使用isset($ fbme ['故乡'] ['名'])? $ fbme ['故乡'] ['名']:'',
    '利益'= GT; $信息[0] ['利益'],
    '区域'=> $信息[0] ['区域设置'],
    电影= GT; $电影,
    音乐= GT; $音乐,
    '政治'= GT; $信息[0] ['政治'],
    relationship_status'=> $信息[0] ['relationship_status'],
    '性'=>使用isset($ fbme ['性别'])? $ fbme ['性别']:'',
    '电视'=> $电视,
    状态=> 0,
    创造= GT; $现在,
    '更新'=> $现在,
);

我试图就如何做到这一点谷歌搜索和所有我能找到的是资料,说明我的阵列需要插入到表之前被分裂。它是否正确?很抱歉的naivity,很新的PHP。


解决方案

您不能插入阵列直接的的mysql 的作为的的mysql 的不理解 PHP 的数据类型。 Mysql的的既懂的 SQL 的。因此,要插入一个数组,你必须将其转换为SQL语句中的MySQL数据库。这可以手动或由库来完成。输出应该是插入语句。

下面是一个标准的MySQL INSERT语句。

  INSERT INTO TABLE1(COLUMN1,COLUMN2,...)VALUES(值1,值..)

如果您的有名称的表 fbdata 与是$ P $在阵列的钥匙psented 的你可以用插入的列这个小片段。这里是你的数组如何转换成这种说法。

  $列=破灭(,array_keys($ insData));
$ escaped_values​​ = array_map('mysql_real_escape_string',array_values​​($ insData));
$值=破灭(,,$ escaped_values​​);
$的SQL =INSERT INTO`fbdata`($列)VALUES($值);

更新PHP7

由于PHP 5.5 mysql_real_escape_string 已去precated和PHP7它已被删除。请参阅: Php.net的文档上的新程序

I have the following array I want to store in my database...

$insData = array(
    'uid' => $fbme['id'],
    'first_name' => $fbme['first_name'],
    'last_name' => $fbme['last_name'],
    'email' => isset($fbme['email']) ? $fbme['email'] : '',
    'link' => $fbme['link'],
    'affiliations' => $networks,
    'birthday' => $info[0]['birthday_date'],
    'current_location' => isset($fbme['location']['name']) ? $fbme['location']['name'] : '',
    'education_history' => $education,
    'work' => $workInfo,
    'hometown_location' => isset($fbme['hometown']['name']) ? $fbme['hometown']['name'] : '',
    'interests' => $info[0]['interests'],
    'locale' => $info[0]['locale'],
    'movies' => $movies,
    'music' => $music,
    'political' => $info[0]['political'],
    'relationship_status' => $info[0]['relationship_status'],
    'sex' =>  isset($fbme['gender']) ? $fbme['gender'] : '',
    'tv' => $television,
    'status' => '0',
    'created' => $now,
    'updated' => $now,
);

I've tried searching google on how to do this and all I can find is information stating my array needs to be split, before inserting into the table. Is this correct? Sorry for the naivity, very new to php.

解决方案

You can not insert an array directly to mysql as mysql doesn't understand php data types. Mysql only understands SQL. So to insert an array into a mysql database you have to convert it to an sql statement. This can be done manually or by a library. The output should be an INSERT statement.

Here is a standard mysql insert statement.

INSERT INTO TABLE1(COLUMN1, COLUMN2, ....) VALUES (VALUE1, VALUE2..)

If you have a table with name fbdata with the columns which are presented in the keys of your array you can insert with this small snippet. Here is how your array is converted to this statement.

$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO `fbdata`($columns) VALUES ($values)";

Update for PHP7

Since PHP 5.5 mysql_real_escape_string has been deprecated and as of PHP7 it has been removed. See: Php.net's documentation on the new procedure.

这篇关于将数组转换成MySQL数据库和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆