|
11-05-2005, 04:18 PM | #1 |
Green Mole
Join Date: Nov 2005
Posts: 4
|
Array variables in MySQL UPDATES
I'm trying to use a MYSQL fieldname as a key in an indexed array .. Maybe there is something basic I'm missing?
The statement : $sql13 = mysql_query("UPDATE $table1 SET recored1 = '$arrayvalue[ID]' " ); just refuses to place the correct arrayvalue in the table1. (no failure .. it just places a o.oo value in the records. ID is in integer record in $table1. $arrayvalue is an indexed array with a 100 or so values Not sure if i'm very dumb or just too smart (prob the former) ? |
11-06-2005, 12:15 PM | #2 |
Head Mole
Join Date: May 2003
Posts: 2,539
|
Assuming $arrayvalue['ID'] contains a legitimate value beforehand, try the following query:
Code:
$sql13 = mysql_query("UPDATE $table1 SET recored1 = '".$arrayvalue['ID']."'");
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension. |
11-08-2005, 01:59 PM | #3 |
Green Mole
Join Date: Nov 2005
Posts: 4
|
I tried using the suggested notation <THEVALUE = '".$arrval['ID']."'> but no go ... HMMMM something really basic here that is escaping me .. Attached is a working program that demonstrates the problem .. Basically I see that the basic question is "How do you copy an array into a table?" ..
<?php echo " learn about arrays <p>"; /* declare some relevant variables */ $DBhost = "localhost"; $DBuser = "bob"; $DBpass = "uhoo_babyloo"; $DBName = "thetest_db"; $table = "temptable"; $arrval = array(.01,101,202,303,404,5.05,6.06,7,8.08,9.09,10.1); mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); echo "found the database successfully <br>"; $sql2 = mysql_query("DROP TABLE $table") or die ('DROP table failed'); $sq14 = mysql_query("CREATE TABLE $table (ID INT UNSIGNED NOT NULL, THEVALUE decimal(8,2)NOT NULL, PRIMARY KEY (ID))") or die ("Create table failed"); $sql5 = mysql_query("INSERT into $table(ID) VALUES ('0')") or die ("INSERT failed"); $sql6 = mysql_query("INSERT into $table(ID) VALUES ('1')") or die ("INSERT failed"); // .......finished creating a sample simple table............................... // .......now try to copy the array values into the table .. using subscripts .. for ($i=0; $i<2; $i++)// this works but it is crazy way to do it &^%$# ! { $sql10 = mysql_query("update $table set THEVALUE = '$arrval[$i]' where (ID = $i) "); } //$sql11 = mysql_query("update $table set THEVALUE = '$arrval[ID]' "); // this fails //$sql12 = mysql_query("update $table set THEVALUE = '$arrval['ID']' "); // this fails //$sql13 = mysql_query("update $table set THEVALUE = '".$arrval['ID']."' "); // this fails //$sql14 = mysql_query("update $table set THEVALUE = '".$arrval[ID]."' "); // this fails $sql20 = mysql_query("select * FROM $table") or die ("Query 12 failed"); while($row=mysql_fetch_row($sql20)) echo "<br>$row[0] .... $row[1]<br>\n"; |
11-08-2005, 02:18 PM | #4 |
Green Mole
Join Date: Nov 2005
Posts: 4
|
one more thing
it is clear from the test program .... that the problem is that PHP/MYSQL won't accept a fieldname as a subscript to an array variable .. in an SELECT update statement .. Mannnnnn I tried every sort of syntax I could find and *&^%$ zip .. nothin' works .. Now , maybe if I really understood it or had some formal training I could get it ! darn
|
11-10-2005, 09:59 PM | #5 |
Head Mole
Join Date: May 2003
Posts: 2,539
|
The $arrval['ID'] doesn't have a value because the $arrval array doesn't have an ID key. With the $arrval array set as follows:
Code:
$arrval = array(.01,101,202,303,404,5.05,6.06,7,8.08,9.09,10.1); Code:
$arrval = array(0 => .01, 1 => 101, 2 => 202, 3 => 303, 4 => 404, 5 => 5.05, 6 => 6.06, 7 => 7, 8 => 8.08, 9 => 9.09, 10 => 10.1); Code:
$arrval = array("ID_0" => .01, "ID_1" => 101, "ID_2" => 202, "ID_3" => 303, "ID_4" => 404, "ID_5" => 5.05, "ID_6" => 6.06, "ID_7" => 7, "ID_8" => 8.08, "ID_9" => 9.09, "ID_10" => 10.1); Code:
mysql_query("UPDATE $table SET THEVALUE = ".$arrval['ID_0']);
__________________
Responses are offered on a voluntary if/as time is available basis, no guarantees. Double posting or bumping threads will not get your question answered any faster. No support via PM or email, responses not guaranteed. Thank you for your comprehension. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Passing variables through crons | CrazyPhil | Coding & Tutorials | 0 | 05-13-2007 09:43 AM |
Indexing Password Protected pages (using session variables) | apetersen | How-to Forum | 1 | 03-27-2007 05:18 AM |
Search Template/Array | tomas | How-to Forum | 1 | 02-29-2004 12:24 AM |
Hide variables | shinji | How-to Forum | 1 | 01-25-2004 08:59 AM |
Cron jobs-Automated Updates | siliconkibou | How-to Forum | 1 | 12-23-2003 07:51 AM |