Jump to content

Function help!


kevdotbadger

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/53920-function-help/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/53920-function-help/#findComment-266605
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.