Email from database using PDO

I have been learning php for a while (david powers book) but I have gotten confused with PDO
I’m trying to write an email script that selects addresses from a database

I used to do it this way:
$email = array();
while($row = mysql_fetch_array($sql)) {
$email[] = $row[‘address’];
}
and I can’t for the life of me fathom how to do this using PDO
I appreciate this is novice stuff so is why I posted in the beginners section and yes I have searched high and low for how to do this?
thanks

It’s hard to tell you exactly how to do it since you only showed us how you “use to” do it and not the code you are currently using. Doing this in PDO is pretty straight forward. The vars I use may not match up to yours so you’ll have to adjust for that, but here’s the basic idea.
[php]
$get = $db->query(“YOUR SQL STATMENT”);
while($row = $get->fetch(PDO::FETCH_ASSOC)) {
$email[] = $row[‘address’];
}
[/php]

Hi Fastsol - thanks for the reply.
I kept it short as I didn’t expect anyone to write it for me. I’m still stuck so any advice would be great. I started down the route of PDO on my website and I have come unstuck with this which gives me the message “could not select members” so I need to re-write it but I am hopeless at languages. I did something similar to this a while ago which I originally borrowed from another forum:

<?php $username = "****"; $password = "****"; $hostname = "localhost"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("members",$dbhandle) or die("Could not select members
"); $sql = mysql_query("select email from members"); $recipients = array(); while($row = mysql_fetch_array($sql)) { $recipients[] = $row['email']; } $sql = mysql_query("select message from members"); $message = array(); while($row = mysql_fetch_array($sql)) { $message[] = $row['message']; } $to = '[email protected]'; $subject = "Latest Updates"; $body = ($message) . "\r\n"; $headers = 'From: [email protected]' . "\r\n" ; $headers .= 'Reply-To: [email protected]' . "\r\n"; $headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n"; mail($to, $subject, $body, $headers); ?>

Not really sure what you are asking. You wanted a solution in PDO which I gave but now you’re posting code that has no PDO in it and now asking about a db connection. Replace this line of code and see what it tells you is the issue.
[php]$selected = mysql_select_db(“members”,$dbhandle)
or die(“Could not select members
”.mysql_error());[/php]

sorry fastsol I wasn’t very clear so thank you for your patience

the code above in mysqli was something I used a while ago
the host I am now using prefers PDO

I tried what you suggested above in PDO but I’m still having problems with it and the PHP error is ‘a query of a non-object’

If you simply placed my code in the code you recently posted, then yes it would fail. You need to change the entire db connection to use PDO, you can’t just swap between the different kinds.
Try this in place of your current connection and while loops.
[php]
$db = = new PDO(‘mysql:host=’.$hostname.’;dbname=members’, $username, $password);

$get = $db->query(“SELECT email FROM members”);
$recipients = array();

while($row = $get->fetch(PDO::FETCH_ASSOC)) {
$recipients[] = $row[‘email’];
}

$get = $db->query(“SELECT message FROM members”);
$message = array();

while($row = $get->fetch(PDO::FETCH_ASSOC)) {
$message[] = $row[‘message’];
}
[/php]

Hi Fastsol
just popped back to say thank you. I had to use bindColumns to stop the error of an unspecified object but I wouldn’t have got it working without being pointed in the right direction - appreciated mate

I don’t think that sounds right, can you post the code you ended up with.

Sponsor our Newsletter | Privacy Policy | Terms of Service