Pari. Pari. - 1 month ago 8
MySQL Question

Adding a database result to an array and fill another respective array to the same database table

I have written a program to insert an array in all the rows of a column in a database table with respect to already filled column in the same database same table.

But it doesn't seems to work properly. Please tell me where I am going wrong!

$query = $sql = "SELECT A FROM `table`";
$result1 = mysql_query($query);
while ($row = mysql_fetch_row($result1)) {

foreach($output as $value){
$count=sizeof($value);

for($counter=0;$counter<$count;$counter++)
{

$query= "INSERT INTO `TABLE` (`B`) VALUES ('" . $value . "') WHERE A= $row[$counter] ";

$insert_col= mysql_query($query);

echo $query;
echo '<br/><br/>';
}}

}


Where
$output
is an array contains the array value as follows:

$output[0]= RD 3.56 , $output[1]= RD 5.56 , $output[2]= RD 4.86


When I run my program, to check my the query .. the output comes like this:

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_1

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_2

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_3

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.60') WHERE A= 3r_4

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.144') WHERE A= 3r_5

INSERT INTO `TABLE` (`B`) VALUES (' RD: 2.436') WHERE A= 3r_5

INSERT INTO `TABLE` (`B`) VALUES (' RD: 1.624') WHERE A= 3r_5


Please help! i am going mad with this bug in my program for almost more than a week.

The output should look like this

INSERT INTO 'TABLE' SET B = ' RD: 1.144' WHERE A = '3r_1'

INSERT INTO 'TABLE' SET B = ' RD: 2.436' WHERE A = '3r_2'

INSERT INTO 'TABLE' SET B = ' RD: 1.624' WHERE A = '3r_3'

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_4'

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_5'

INSERT INTO 'TABLE' SET B = ' RD: 1.560' WHERE A = '3r_6'


The problem here is
$row
value is repeated 6 times for all the
$output
values. How can I solve the problem?

And after lot of trials, I got to know that the problem is in calling the arrays. So, if anyone can tell me how to call the two arrays at the same time w.r.t the code written above, I would be very grateful.

Answer

From what I understand, you have a looping problem.

You have 2 lists that you need to increment simultaneously.

Try this:

$value = [' RD: 1.144', ' RD: 2.436', ' RD: 1.624', ' RD: 1.560', ' RD: 1.560', ' RD: 1.560'];
$row = ['3r_1', '3r_2', '3r_3', '3r_4', '3r_5', '3r_6'];

$cnt = count($row);//assuming $row and $value are of the same size

for($i = 0; $i < $cnt, $i++) {
    echo "INSERT INTO 'TABLE' SET B = '$value[$i]' WHERE A = '$row[$i]' ";
}

This is a simpler example, but essentially, this is what you need to be doing.

Comments