我的任务是将硬编码字段转换为动态字段。我已将其部分更改为动态字段
让我给你解释一下当时的情况
我们有很多数据库,每个数据库都有一个名为Survey的表
通过使用DESCRIBE语句,我们将检索测量表中的字段,而不管数据库是什么。
我需要知道如何一次又一次地循环,直到测量表中的所有字段都出现。
在下面的代码中,我将for循环留空了。
请让我知道需要做哪些更改才能使其正常工作
我真的很感谢任何形式的帮助
function insertIntoUserUploadFileds() {
$describe="DESCRIBE surveys";
$sql = "INSERT INTO `userUploadFields` (`fieldName`, `inUse`, `mandatory`, `type`, `mapTo`) VALUES";
$inUse="0";
$type="";
//for(){
if($field=='type'){
$type="N";
}elseif(($field=='fname') || ($field=='lname') || ($field=='phone')){
$inUse="1";
$type="T";
}elseif($field=='email'){
$inUse="1";
$type="E";
}
//$sql .= "('".$field."', '".$inUse."', '0', '
$result1 = mysql_query ($describe);
$result = mysql_query ($sql);
//}
}发布于 2012-11-16 11:50:19
$result1 = mysql_query ('DESCRIBE surveys');
//here is how you retieve all field and check
while($row = mysql_fetch_array($result1)) {
$sql = "INSERT INTO `userUploadFields` (`fieldName`, `inUse`, `mandatory`, `type`, `mapTo`) VALUES";
//here you can do if else to check the column name
if($row['field']=='type')
{
$type="N";
}
else if(($row['field']=='fname') || ($row['field']=='lname') || ($row['type']=='phone'))
{
$inUse="1";
$type="T";
}
else ($row['field']=='email')
{
$inUse="1"
$type="E";
}
//build your query
$sql .= "('".$field."', '".$inUse."', '0', '......)
//execute your complete query
$result = mysql_query ($sql);
}//end of while发布于 2012-11-16 12:02:03
如果您正在尝试检索特定列的默认type,则可以查看以下内容,而不是使用DESCRIBE。它描述了如何分解来自特定表的信息。Codex
https://stackoverflow.com/questions/13410043
复制相似问题