Jump to content

[SOLVED] Help creating php & MySQL query & save data in an array ...


bsamson

Recommended Posts

Have a what seems to be simple problem ....

 

I have a DB table that looks like this:

 

Database: MAIN

Table: STORE

 

Fields:

ID  Location-Name

34  Albany

58  Chicago

45  Atlanta

 

I need to create an array thats map to the ID #. For example, I need to create the PHP and MySQL Query that does this:

 

$locname[34] = Albany

$locname[58] = Chicago

etc ... etc ... etc ...

 

This is the code that I have ... that obviously doesn't work:

 

<?php
$db2con = "main";
include "../../scripts/c2d.php";
$queryMAIN = mysql_query("SELECT * FROM stores") or die(mysql_error());	 
$numstores=mysql_num_rows($queryMAIN);
$row = mysql_fetch_assoc($queryMAIN);

for ($ct=1;$ct<$numstores+1;$ct++) {
$storeid = $row[id];
$storename = $row[location];
$locname[$storeid] = $storename;

$storeid = "";
$storename = "";
}
?>

 

I know my logic is flawed because I am not sure how to change the rows ... Please if I can get some help writing this I will appreciated it!

Try:

 

<?php
$db2con = "main";
include "../../scripts/c2d.php";
$queryMAIN = mysql_query("SELECT * FROM stores") or die(mysql_error());	 
$numstores=mysql_num_rows($queryMAIN);
$locname = array();
if($numstores > 0){
while($row = mysql_fetch_assoc($queryMAIN)){
	$storeid = $row['id'];
	$storename = $row['location'];
	$locname[$storeid] = $storename;		
}
}
echo '<pre>'.print_r($locname,1).'</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

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