Php 将数组保存到多行中
嘿,我怎样才能将数组保存到数据库中的多行中呢?我正在用php使用mysql数据库Php 将数组保存到多行中,php,mysql,curl,Php,Mysql,Curl,嘿,我怎样才能将数组保存到数据库中的多行中呢?我正在用php使用mysql数据库 // GETTING ALL THE B NODE STUFFS AND PRINTING IT'S CONTENTS $result = array(); foreach($document->getElementsByTagName('b') as $node){ $result[preg_replace('/:\s+$/','',$node->textContent)] =
// GETTING ALL THE B NODE STUFFS AND PRINTING IT'S CONTENTS
$result = array();
foreach($document->getElementsByTagName('b') as $node){
$result[preg_replace('/:\s+$/','',$node->textContent)] = trim($node->nextSibling->textContent);
}
var_dump($result);
这是我从阵列中提取的结果
array
'Choose by Subject Category or Module Code' => string '' (length=0)
'
Back to Home page' => string '' (length=0)
'International' => string 'visiting students should consult the' (length=36)
'Undergraduate' => string 'students should refer to the relevant section of the UCC' (length=56)
'Postgraduate' => string 'students should refer to the relevant section of the UCC' (length=56)
'Credit Weighting' => string '5' (length=1)
'Teaching Period(s)' => string 'Teaching Period 1.' (length=18)
'No. of Students' => string 'Min 15, Max 30.' (length=15)
'Pre-requisite(s)' => string 'None' (length=4)
'Co-requisite(s)' => string 'None' (length=4)
'Teaching Methods' => string '1 x 4hr(s) Lectures; Other (Distance Education Module - Up to 146hrs Self Directed Study).' (length=90)
'Module Co-ordinator' => string 'Dr Peter Cleary, Department of Accounting, Finance and Information Systems.' (length=75)
'Lecturer(s)' => string 'Staff, Department of Accounting, Finance and Information Systems.' (length=65)
'Module Objective' => string 'To examine the management uses of accounting information and to enhance students ability to exert effective managerial control.' (length=127)
'Module Content' => string 'Topics include; the accounting information needs of management, costs and pricing; estimating costs; the identification of key performance indicators; budgeting for control; capital investment appraisal and implications for strategic planning and control.' (length=256)
'Learning Outcomes' => string 'On successful completion of this module, students should be able to:' (length=68)
'Assessment' => string 'Total Marks 100: Continuous Assessment 100 marks (Project/ Essay. Approximately 1500 words.).' (length=93)
'Compulsory Elements' => string 'Continuous Assessment.' (length=22)
'Penalties (for late submission of Course/Project Work etc.)' => string 'Where work is submitted up to and including 7 days late, 10% of the total marks available shall be deducted from the mark achieved. Where work is submitted up to and including 14 days late, 20% of the total marks available shall be deducted from the mark achieved. Work submitted 15 days late or more shall be assigned a mark of zero.' (length=336)
'Pass Standard and any Special Requirements for Passing Module' => string '40%.' (length=4)
'End of Year Written Examination Profile' => string 'No End of Year Written Examination.' (length=35)
'Requirements for Supplemental Examination' => string 'Marks in passed element(s) of Continuous Assessment are carried forward, Failed element(s) of Continuous Assessment must be repeated (Resubmission of revised Continuous Assessment).' (length=181)
我确实尝试了一个包含NSERT查询的for-each语句,但所有内容都保存在一个列中
$array_data = implode("array_separator", $result);
foreach($result as $snode){
$query = sprintf("INSERT INTO save_array (ModuleCode) VALUES ('%s')",mysql_real_escape_string($snode));
mysql_query($query) or die('Error, insert query failed');
echo $snode.<br />';
}
echo '<br /><br /><br />';
$array\u data=内爆(“array\u分隔符”,$result);
foreach($snode的结果){
$query=sprintf(“插入到保存数组(ModuleCode)值('%s')”,mysql\u real\u escape\u字符串($snode));
mysql_query($query)或die('Error,insert query failed');
echo$snode.
;
}
回音“
”;
任何帮助都将不胜感激。谢谢
//============== LATEST INSERT QUERY================//
$array_data = implode("array_separator", $result);
foreach($result as $snode){
$query = sprintf("INSERT INTO save_array
(ModuleCode,
Homepage,
International,
Undergraduate,
Postgraduate,
CreditWeighting,
TeachingPeriod,
NoofStudents,
Prerequisite,
Corequisite,
TeachingMethods,
ModuleCoordinator,
Lecturer,
ModuleObjective,
ModuleContent,
LearningOutcomes,
Assessment,
CompulsoryElements,
Penalties,
PassStandard,
EndofYearWrittenExamination,
RequirementsforExamination) VALUES ('%s')",mysql_real_escape_string($snode));
foreach ($result as $key => $value)
$query = $query . "$value";
echo '<br /><br />';
mysql_query($query) or die($query."<br/><br/>".mysql_error());
echo $snode. '<br />';
}
echo '<br /><br /><br />';
/===============================================================/
$array\u data=内爆(“array\u分隔符”,$result);
foreach($snode的结果){
$query=sprintf(“插入到保存数组中
(模块代码,
主页
国际,
大学生
研究生,
信用权重,
教学时期,
没有学生,
先决条件
共济会,
教学方法,
模块协调员,
讲师,
模块对象,
模块内容,
学习成果,
评估,
强制元素,
处罚,
通过标准,
内测,
RequirementsforExamination)值('%s'),mysql\u real\u escape\u字符串($snode));
foreach($结果为$key=>$value)
$query=$query.“$value”;
回音“
”;
mysql\u query($query)或die($query。“
”。mysql\u error();
echo$snode.“
”;
}
回音“
”;
一个快速的答案是迭代foreach循环,将值连接到INSERT查询中(有关更多信息,请参阅),并让foreach循环排除键“industrial”、“International”和“Postdoc”以及任何长度为0的值
您的问题似乎在INSERT查询中。试着这样做:
$query = sprintf("INSERT INTO save_array (CreditWeighting, TeachingPeriod, NoofStudents, etc.)) VALUES ('%s')",mysql_real_escape_string($snode));
foreach ($result as $key => $value)
$query = $query . "$value";
并进行一些调整,以获得正确的查询字符串。您可以在数组的所有元素上循环,并准备SQL INSERT到语句中,当你这样做的时候,执行它。我确实尝试过,但都只在一列中运行。我上面有一个示例代码。嘿,谢谢你的回复。我所说的多行是指每个值都插入到不同的行中,因为我将对大量数据执行此操作,并希望数组中提取的所有数据都单独保存。我不确定我是否正确地解释了这一点。看起来您只需要键值对表,但在这种情况下,您将丢失大量信息。您是否有正在使用的架构(数据库表定义)?我已将我的架构添加到上面编辑的代码中。请查看。回答已编辑,请查看。不幸的是,我没有时间来纠正所有复杂的查询结构,但这是基本的想法。非常感谢。我会提前告诉你,并让你知道我的进展。旁注:尝试使用函数(或PDO)而不是旧函数,它们很快就会被删除。