Kane250 Posted May 6, 2008 Share Posted May 6, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/ Share on other sites More sharing options...
fenway Posted May 6, 2008 Share Posted May 6, 2008 Well, you can push then onto an array in php and join them with commas, or you can use group_concat. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534319 Share on other sites More sharing options...
fenway Posted May 6, 2008 Share Posted May 6, 2008 Well, you can push then onto an array in php and join them with commas, or you can use group_concat. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534322 Share on other sites More sharing options...
Kane250 Posted May 6, 2008 Author Share Posted May 6, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534497 Share on other sites More sharing options...
mdnghtblue Posted May 6, 2008 Share Posted May 6, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534698 Share on other sites More sharing options...
fenway Posted May 6, 2008 Share Posted May 6, 2008 Well aren't I already putting them into an array? No, you're not... that not's what fetch_array() does. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534703 Share on other sites More sharing options...
mendoz Posted May 6, 2008 Share Posted May 6, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534764 Share on other sites More sharing options...
Kane250 Posted May 6, 2008 Author Share Posted May 6, 2008 Thanks guys but neither worked. They both return just the first email address... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534814 Share on other sites More sharing options...
Hooker Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-534989 Share on other sites More sharing options...
Kane250 Posted May 7, 2008 Author Share Posted May 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-535016 Share on other sites More sharing options...
corbo950 Posted May 7, 2008 Share Posted May 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-535020 Share on other sites More sharing options...
fenway Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-535062 Share on other sites More sharing options...
Kane250 Posted May 8, 2008 Author Share Posted May 8, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-536308 Share on other sites More sharing options...
Kane250 Posted May 9, 2008 Author Share Posted May 9, 2008 Is it a problem that I have 3 columns? One email, one first name, and one last name. Could that be stopping it somehow? Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-536672 Share on other sites More sharing options...
fenway Posted May 9, 2008 Share Posted May 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-536696 Share on other sites More sharing options...
Kane250 Posted May 9, 2008 Author Share Posted May 9, 2008 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"> Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-536783 Share on other sites More sharing options...
fenway Posted May 9, 2008 Share Posted May 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-536924 Share on other sites More sharing options...
Kane250 Posted May 9, 2008 Author Share Posted May 9, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-537014 Share on other sites More sharing options...
Kane250 Posted May 10, 2008 Author Share Posted May 10, 2008 bump for hopefully more help ??? Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-537742 Share on other sites More sharing options...
fenway Posted May 11, 2008 Share Posted May 11, 2008 What does mysql_num_rows() have to say? No error messages? Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-537940 Share on other sites More sharing options...
Kane250 Posted May 11, 2008 Author Share Posted May 11, 2008 What does mysql_num_rows() have to say? No error messages? mysql_num_rows($contacts) doesn't print out anything actually... blank Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-538569 Share on other sites More sharing options...
Kane250 Posted May 11, 2008 Author Share Posted May 11, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-538586 Share on other sites More sharing options...
Kane250 Posted May 12, 2008 Author Share Posted May 12, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/104328-solved-having-trouble-pulling-multiple-rows-from-a-db/#findComment-538739 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.