Mutley Posted January 3, 2009 Share Posted January 3, 2009 Hi guys, I've got this: <?php $aUsers = array( "Ädams, Egbert", "Altman, Alisha", "Archibald, Janna", "Auman, Cody", "Bagley, Sheree", "Ballou, Wilmot" ); ?> But I wish to pick the entries up from the database, like the below example, except it doesn't work as I'm assiging a variable to it, just creates a parse error: <?php $aUsers = array( $sql = "SELECT id, zip FROM zipcodes ORDER BY id"; $result = mysql_query($sql); while(list($id, $zip) = mysql_fetch_row($result)) { echo '"'.$id.", ".$zip.'"'.',<br />'; } ); ?> Any thoughts? I know the query I do creates an array anyway I think, just don't know how to get it into the desired format. Thanks, Nick. Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/ Share on other sites More sharing options...
Mchl Posted January 3, 2009 Share Posted January 3, 2009 <?php $sql = "SELECT id, zip FROM zipcodes ORDER BY id"; $result = mysql_query($sql); while(list($id, $zip) = mysql_fetch_row($result)) { $aZips[] = array( "id" => $id, "zip" => $zip, ); } ?> or <?php $sql = "SELECT id, zip FROM zipcodes ORDER BY id"; $result = mysql_query($sql); while(list($id, $zip) = mysql_fetch_row($result)) { $aZips[$id] = $zip; } ?> (you can't have any code within array() ) Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728835 Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 <?php $sql = "SELECT id, zip FROM zipcodes ORDER BY id"; $result = mysql_query($sql); while(list($id, $zip) = mysql_fetch_row($result)) { $aUsers[$id] = $zip; } print_r($aUsers); ?> Maybe that is what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728838 Share on other sites More sharing options...
Mutley Posted January 3, 2009 Author Share Posted January 3, 2009 @premiso, Doesn't seem to be returning any results in the array for those? @Mchl, Syntax error? Thanks, Nick. Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728848 Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 Are you sure you had data in your database? And that you have a valid SQL connection? <?php $sql = "SELECT id, zip FROM zipcodes ORDER BY id"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_row($result)) { $aUsers[$row['id']] = $row['zip']; } print_r($aUsers); ?> That should work as long as you have a valid connection and data in the database. Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728851 Share on other sites More sharing options...
Mutley Posted January 3, 2009 Author Share Posted January 3, 2009 Cheers guys, got that working! Just 1 more thing, is it possible to create 2 arrays with an INNER JOIN? <?php $sql = "SELECT streets.id, streets.street zipcodes.zip FROM streets INNER JOIN zipcodes ON streets.zip = zipcodes.id ORDER BY streets.id"; $result = mysql_query($sql); while(list($id, $street, $zip) = mysql_fetch_row($result)) { $aUsers[$id] = $street; $aInfo[$id] = $zip; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728958 Share on other sites More sharing options...
Mchl Posted January 3, 2009 Share Posted January 3, 2009 As long as the query is correct, it should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/139346-creating-php-array-from-mysql/#findComment-728961 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.