Jump to content

[SOLVED] having trouble pulling multiple rows from a db


Kane250

Recommended Posts

I have a database where I want to pull email addresses from one column, and I want it to display every one separated by commas, so I can insert it for email.

 

My code is incomplete I'm sure..I'm new to MySQL, and hung up on how I have to pull these from the table.

 

$connectsql = mysql_connect('localhost', 'user', 'pw') 
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";

$db_selected = mysql_select_db('user', $connectsql);

//Call Saved Email contacts to the email
$mlistcontacts = mysql_query('SELECT email FROM emaillist WHERE emailID>0');
$mlistcontacts = mysql_fetch_array ($mlistcontacts);
print $mlistcontacts['email'];

 

When I print this out it just displays the first email address in the table (first row).

 

Thanks in advance for any insight!

Link to comment
Share on other sites

Well aren't I already putting them into an array? That's what I was trying to do, but I also need to be able to call every one in the list. The list will be updated regularly, so some code that would add all emails with an ID>0 into a string seperated by commas is exactly what I need...

Link to comment
Share on other sites

Not sure if this is the most efficient way, but this is how I'd do it:

 

 

$mlistcontacts = mysql_query('SELECT email FROM emaillist WHERE emailID>0');
while($mlistcontacts = mysql_fetch_array ($mlistcontacts))
{
$contacts[$i] = $mlistcontacts[email];
$i++;
}

echo implode(",", $contacts);

Link to comment
Share on other sites

Try this:

 

<?php


// Get the results from the database
$mlistcontacts = mysql_query('SELECT email FROM emaillist WHERE emailID>0');
// You want an associative array, not a numeric, so we'll use mysql_fetch_assoc
while($mlistcontacts = mysql_fetch_assoc ($mlistcontacts))
{
   // You forgot the single quotes around email
   $contacts[$i] = $mlistcontacts['email'];
   $i++;
}

echo implode(",", $contacts);



?>

Link to comment
Share on other sites

Can i ask why you have the WHERE clause? is emailID auto inc or are you just using it as a boolen? because if its auto inc you're checking what exactly?

 

Honestly i've seen mysql get bitchy about less in my time so its worth a try removing it if it is auto inc.

Link to comment
Share on other sites

Can i ask why you have the WHERE clause? is emailID auto inc or are you just using it as a boolen? because if its auto inc you're checking what exactly?

 

Honestly i've seen mysql get bitchy about less in my time so its worth a try removing it if it is auto inc.

 

 

I'm using the WHERE clause becase I want it to pull every entry. Yes it is auto incrementing. Are you saying that I don't need the where clause?

Link to comment
Share on other sites

I'm using the WHERE clause becase I want it to pull every entry. Yes it is auto incrementing. Are you saying that I don't need the where clause?

 

The WHERE clause if for when you dont want to return all of the rows like if you only wanted to return people with a certain value in the table so if you want all of the values you dont want a WHERE clause

Link to comment
Share on other sites

Thanks guys but neither worked. They both return just the first email address...  :-\

That's not possible... echo the value of the counter variable, if it's more than one, then there's something else going on.

Link to comment
Share on other sites

Thanks guys but neither worked. They both return just the first email address...  :-\

That's not possible... echo the value of the counter variable, if it's more than one, then there's something else going on.

 

If the counter variable is $i, which I think it is, then no, it only returns 1, nothing more...

Link to comment
Share on other sites

If the counter variable is $i, which I think it is, then no, it only returns 1, nothing more...

If that's true, then there is only one e-mailID that satifies your WHERE clause.  Drop the WHERE clause, see what happens.  It's not MySQL, it's the data.

Link to comment
Share on other sites

If the counter variable is $i, which I think it is, then no, it only returns 1, nothing more...

If that's true, then there is only one e-mailID that satifies your WHERE clause.  Drop the WHERE clause, see what happens.  It's not MySQL, it's the data.

 

No, even without the where clause, it does the same thing. The counter also only prints 1 as well. Here is my table data, I don't see what's wrong with it?

 

<img src="http://a.parsons.edu/~kalbarron/table.jpg">

Link to comment
Share on other sites

Then either you're not connecting to the right DB, or your code isn't iterating over the rows (your initial code didn't)... but I've been assuming you've followed other suggestions.

 

Post your code.

Link to comment
Share on other sites

Then either you're not connecting to the right DB, or your code isn't iterating over the rows (your initial code didn't)... but I've been assuming you've followed other suggestions.

 

Post your code.

 

I used code that was previously suggested in this topic...it has to be connecting to the database correctly because it pulls one email address out of it, the first row.

 

Thanks for all your help!

 

<?php

//Connect to the database

$connectsql = mysql_connect('localhost', 'user', 'password') 
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";

$db_selected = mysql_select_db('database', $connectsql);

// Get the results from the database
$mlistcontacts = mysql_query('SELECT email FROM emaillist');

// You want an associative array, not a numeric, so we'll use mysql_fetch_assoc
while($mlistcontacts = mysql_fetch_assoc ($mlistcontacts))
{
   // You forgot the single quotes around email
   $contacts[$i] = $mlistcontacts['email'];
   $i++;
}

echo implode(",", $contacts);

print $i;

$to      = $_POST['to'];
$subject = $_POST['subject'];
$message = stripslashes ($_POST['msgpost']);


print "<pre>";
if ($_POST) {
mail($to, $subject, $message, "From: $from\nContent-Type: text/html; charset=iso-8859-1");
}
print "</pre>";
?>

Link to comment
Share on other sites

Fixed (sorta)

 

This:

// Get the results from the database
$mlistcontacts = mysql_query('SELECT email FROM emaillist');

while($mlistcontacts2 = mysql_fetch_assoc($mlistcontacts)){
$massmail = implode ($mlistcontacts2);
print ($massmail);
}

 

Returns each row, but now I'm having issues adding in the commas. I tried adding it in in both the print statement and the implode, and neither are doing it.. If I use mysql_fetch_array it will add the commas, but it will also print each row twice, and it will not have the comma the second time.

 

Ex: email@email.com, email@email.comanotheremail@email.com, anotheremail@email.comnewemail@email.com etc...

Link to comment
Share on other sites

All cleared up. Final Code:

 

<?php// Get the results from the database
$mlistcontacts = mysql_query('SELECT email FROM emaillist');

while($mlistcontacts2 = mysql_fetch_assoc($mlistcontacts)){
$massmail[] = $mlistcontacts2['email'];
}
print implode(",", $massmail);?>

 

Thanks for everyone's help!

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.