Jump to content

store fetched values in array -> update into mysql table


jakebur01
 Share

Recommended Posts

I am pulling 5 addresses and I am wanting to store these 5 addresses in columns in a separate table.  On the query string I put LIMIT 5.  How can I store the results of each of the 5 loops into columns in a separate table?  The columns are A1(for name), A2(for address), A3 (for city), A4 (for state), A5 (for zip), A6 (for phone), A7 (for miles),  .... then I also have B1-B7, C1-C7, D1-D7, and E1-E7.

 

I am basically wanting to store up to 5 addresses in an array and then update them into a mysql table.

 

 

foreach ($zips as $key => $value) {
  
$result = mysql_query("SELECT State FROM extreme WHERE Zip = '$key' AND Type='store' AND `InStore` <> 'NO' LIMIT 5", $db);
$num_rows = mysql_num_rows($result);

while($myrow = mysql_fetch_array ($result))
{


$Dealer=$myrow['Dealer'];
$Address=$myrow['Address'];
$City=$myrow['City'];
$State=$myrow['State'];
$Zip=$myrow['Zip'];
$Phone=$myrow['Phone'];
$Miles = $value;


}

}

 

Link to comment
Share on other sites

This is an odd thing to do, but there are several ways to do it. You might try doing it in the query:

 

INSERT INTO other
(A1,A2,A3,A4,A5,A6,A7,B1,B2,B3,B4,B5,B6,B7,C1,C2,C3,C4,C5,C6,C7,D1,D2,D3,D4,D5,D6,D7,E1,E2,E3,E4,E5,E6,E7)
SELECT Name, Address, City, State, Zip, Phone, Miles FROM extreme
WHERE Zip = '$key' AND Type = 'store' AND InStore <> 'NO' LIMIT 5

Link to comment
Share on other sites

I really would like to do something like this:

UPDATE TABLE SET `A1`=$myrow["0"][0], `A2`=$myrow["0"][1], `A3`=$myrow["0"][2], `A4`=$myrow["0"][3], `A5`=$myrow["0"][4], `A6`=$myrow["0"][5], `A7`=$myrow["0"][6], `B1`=$myrow["1"][0], `B2`=$myrow["1"][1], `B3`=$myrow["1"][2], `B4`=$myrow["1"][3], `B5`=$myrow["1"][4], `B6`=$myrow["1"][5], `B7`=$myrow["1"][6], etc...... WHERE `id`='$rowid'

 

Is this possible?

Link to comment
Share on other sites

I really would like to do something like this:

UPDATE TABLE SET `A1`=$myrow["0"][0], `A2`=$myrow["0"][1], `A3`=$myrow["0"][2], `A4`=$myrow["0"][3], `A5`=$myrow["0"][4], `A6`=$myrow["0"][5], `A7`=$myrow["0"][6], `B1`=$myrow["1"][0], `B2`=$myrow["1"][1], `B3`=$myrow["1"][2], `B4`=$myrow["1"][3], `B5`=$myrow["1"][4], `B6`=$myrow["1"][5], `B7`=$myrow["1"][6], etc...... WHERE `id`='$rowid'

 

Is this possible?

This will do it:

 

while($row = mysql_fetch_array ($result, MYSQL_NUM)) {
   $myrow[] = $myrow;
}

But your select will have to be in this order:

SELECT Name, Address, City, State, Zip, Phone, Miles FROM extreme

 

And your insert will need to look like this with proper quoting:

UPDATE TABLE SET `A1`='{$myrow[0][0]}', `A2`='{$myrow[0][1]}' . . .

 

It may be better to do it with the column names as the array indexes so that it is more clear and you don't have to worry about the select order:

 

while($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
   $myrow[] = $myrow;
}

UPDATE TABLE SET `A1`='{$myrow[0]['Name']}', `A2`='{$myrow[0]['Address']}' . . .

Link to comment
Share on other sites

It is updating the first row of callbacks table columns A1-A6 and that is all it is updating.  For some reason it is not storing B1-E7 in the array and it is not looping through the other rows.

foreach ($zips as $key => $value) {

  $result = mysql_query("SELECT Dealer, Address, City, State, Zip, Phone FROM extreme WHERE Zip = '$key' AND Type='store' AND `InStore` <> 'NO' LIMIT 5", $db);



while($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
   $myrow[] = $row;


   }
   
mysql_query("UPDATE callbacks SET `A1`='{$myrow[0]['Dealer']}', `A2`='{$myrow[0]['Address']}', `A3`='{$myrow[0]['City']}', `A4`='{$myrow[0]['State']}', `A5`='{$myrow[0]['Zip']}', `A6`='{$myrow[0]['Phone']}', `B1`='{$myrow[1]['Dealer']}', `B2`='{$myrow[1]['Address']}', `B3`='{$myrow[1]['City']}', `B4`='{$myrow[1]['State']}', `B5`='{$myrow[1]['Zip']}', `B6`='{$myrow[1]['Phone']}', `C1`='{$myrow[2]['Dealer']}', `C2`='{$myrow[2]['Address']}', `C3`='{$myrow[2]['City']}', `C4`='{$myrow[2]['State']}', `C5`='{$myrow[2]['Zip']}', `C6`='{$myrow[2]['Phone']}', `D1`='{$myrow[3]['Dealer']}', `D2`='{$myrow[3]['Address']}', `D3`='{$myrow[3]['City']}', `D4`='{$myrow[3]['State']}', `D5`='{$myrow[3]['Zip']}', `D6`='{$myrow[3]['Phone']}', `E1`='{$myrow[4]['Dealer']}', `E2`='{$myrow[4]['Address']}', `E3`='{$myrow[4]['City']}', `E4`='{$myrow[4]['State']}', `E5`='{$myrow[4]['Zip']}', `E6`='{$myrow[4]['Phone']}' WHERE `id`='$callid'", $db);
   

}

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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