我創建了一個名爲「test」的數據庫並創建了一個名爲「biodata」的表。我已經創建了3個名爲「名稱」「年齡」和「描述」的列到生物數據表中。現在如何將數組結果存儲到每列中。如何使用PHP將多維數組存儲到mysql
下面是完整的代碼...
<?php
$ip = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$res = mysql_connect($ip,$username,$password);
if(!$res)
{
echo "DB Connection Failed.";
exit;
}
if(!mysql_select_db("test"))
{
echo "NOT SELECTED";
exit;
}
$company = array(
'Record1'=>array('Shabbir',26,'Designer'),
'Record2'=>array('Burhan',24,'Architecture'),
'Record3'=>array('Huzeifa',20,'Accountant'),
);
foreach ($company as $employees=>$details){
echo '<strong>'.$employees.'</strong><br>';
foreach($details as $employeeinfo){
echo $employeeinfo.'<br>';
}
}
$sql = "INSERT INTO biodata (Name, Age, Description) VALUES ($employeeinfo[0], $employeeinfo[1], '$employeeinfo[2]')";
mysql_query($sql);
?>
偉大的作品!這工作就像一個魅力.. – 2012-04-17 19:57:04
可以請你解釋一下爲什麼你在VALUES中使用'%s'? – 2012-04-17 19:58:40
我試圖正確地格式化值..見http://php.net/manual/en/function.sprintf.php有關'sprintf'的更多信息...我很高興它爲你工作...不' t忘記接受 – Baba 2012-04-17 20:02:36