Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assoc Array madness

Status
Not open for further replies.

salewit

Programmer
Oct 31, 2002
58
US
Newbie question here... I'm sure it's so simple, but I'm getting a massive migraine from it.

I've got a MySQL database with this info in it:

Model Qty
--------------
1120-7 5
1120-2 23
2838-1 3

I want to assign this into an array out of the database as such:

$ytd = array ("1120-7" => "5", "1120-2" => "23 etc

I just can't figure it out. If I use mysql_fetch_assoc, I get $ytd[model]

If I use mysql_fetch_row and then a foreach loop, I can only pull out one element at a time and can't seem to assign them.

This is super easy I know... I just can't figure it out.

Also, as a side note and not important to this problem... the MySQL statement I'm using is "select model,sum(qty)..." When I use the fetch_assoc, it attempts to assign sum(qty) as $ytd[sum(qty)] but when I try do say print something like that ( print $ytd[sum(qty)] ) , I get an error.
 
try using "select * from ..." and then when you fetch the row using the mysql_fetch_row command then you will get an array with the 2 values ie:
Code:
$query = mysql_query("select * from table1");
$rows = mysql_num_rows($query);
for ($i=0;$i<$rows;$i++)
{
  $line = mysql_fetch_row($query);
  $var1 = $line[0];
  $var2 = $line[1];
  ..........  
}
for the 1st line the $line variable will hold 1120-7 and 5
what you then do with the values is up to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top