Jump to content

Need help with creating an array


furn355

Recommended Posts

 $mailList = array (
							
 'name1' => '[email protected]',
 'name 2' => '[email protected]',
							
 );

Is what i'm trying to do, but i want to populate it from my DB.

 

I have tried this:

$list = mysql_query("SELECT name, email FROM ".$_POST['db1']."");
	$mailList = array();
	while($data = mysql_fetch_array($list)) {
	    $mailList[$list['name']] =$list['email'];
	}
						
echo '<br /> var dump <br />';
						
	var_dump($mailList);
						
echo '<br /> var dump <br />';

but it just returns : 

 

array(1) { [""]=> NULL }

 

Any help would be greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/279785-need-help-with-creating-an-array/
Share on other sites

$data holds the query row in your loop not $list

$list = mysql_query("SELECT name, email FROM ".mysql_real_escape_string($_POST['db1']));
$mailList = array();
while($data = mysql_fetch_assoc($list)) 
{
	// probably better to use email as the array key as it is unique
	$mailList[$data['email']] = $data['name'];
}
						
echo '<br /> var dump <br />';
						
	var_dump($mailList);
						
echo '<br /> var dump <br />';

Always do error checking.  Your query statement is flawed.  If you had checked your query results and displayed MySQL_error you would have caught the error before trying to process an non-existent result.  (In case you didn't notice that Neil corrected your query.)

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.