Jump to content

Recommended Posts

 $mailList = array (
							
 'name1' => 'name1@googlemail.com',
 'name 2' => 'name2@tld.com',
							
 );

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.)

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.