kevdotbadger Posted June 2, 2007 Share Posted June 2, 2007 I made a simple function to dymanically create a SQL query. <?php function AddDataToTable(){ $HowMany = func_get_arg(0); $x = func_get_arg(0); $TableName = func_get_arg(1); $q1 = "INSERT INTO $TableName ("; while($HowMany > 0){ $Name = func_get_arg($HowMany + $HowMany); $Value = func_get_arg($HowMany + $HowMany + 1); $q2 = ''; if($x == 1){ $q2 = "'$Name'"; }else{ $q2 = "'$Name',"; } $HowMany = $HowMany - 1; } $q3 = ")values("; $HowMany = func_get_arg(0); while($HowMany > 0){ $Name = func_get_arg($HowMany + $HowMany); $Value = func_get_arg($HowMany + $HowMany + 1); $q4 = ''; if($x == 1){ $q4 = "'{$Value}'"; }else{ $q4 = "'{$Value}',"; } $HowMany = $HowMany - 1; } $q5 = ")"; echo $q1.$q2.$q3.$q4.$q5; }; Now when i run the function with... AddDataToTable(4, name_table, name, kevin, age, 19, email, hhh, address, myhouse); the output displays INSERT INTO name_table ('name',)values('kevin',) It doesnt display the other values. Any idea why? And how i would output all the values? Quote Link to comment https://forums.phpfreaks.com/topic/53920-function-help/ Share on other sites More sharing options...
penguin0 Posted June 2, 2007 Share Posted June 2, 2007 i dont think this is right: '{$Value}' why do you need the {} for value and not for name? They are essentially the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/53920-function-help/#findComment-266601 Share on other sites More sharing options...
kevdotbadger Posted June 2, 2007 Author Share Posted June 2, 2007 no no no. Thats all fine. I think the problem lies in if($x == 1){ $q4 = "'{$Value}'"; }else{ $q4 = "'{$Value}',"; } Now what i want to do is change the else branch so that the $q4 is an array and each time it loops around the loop it adds to the array. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/53920-function-help/#findComment-266605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.