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!

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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