Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.