bsamson Posted January 20, 2008 Share Posted January 20, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/86938-solved-help-creating-php-mysql-query-save-data-in-an-array/ Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86938-solved-help-creating-php-mysql-query-save-data-in-an-array/#findComment-444469 Share on other sites More sharing options...
bsamson Posted January 20, 2008 Author Share Posted January 20, 2008 Works Great GingerRobot! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/86938-solved-help-creating-php-mysql-query-save-data-in-an-array/#findComment-444476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.