What is the simplest way to return a ROW as well as loop through the ROWS with PDO?

  • Thread starter Thread starter t
  • Start date Start date

t

New Member
If I am doing an old query to return a row I would do something like this:\[code\]$sql = 'SELECT id FROM table WHERE email="' . mysql_real_escape_string($email) . '" LIMIT 1';$res = mysql_query($sql);$row = mysql_fetch_array($res);echo $row['id'];\[/code\]How do I do that with a Prepared Statement? I can get this far...\[code\] $stmt = $dbh->prepare("SELECT id FROM table WHERE email = ? LIMIT 1"); if ($stmt->execute(array($_POST['email']))) { // what goes in here to pull out this one row? }\[/code\]Secondly, if I have multiple rows I would do it like this:\[code\]$sql = 'SELECT id FROM table WHERE email="' . mysql_real_escape_string($email) . '" ';$res = mysql_query($sql);while($row = mysql_fetch_array($res)) { echo $row['id'];}\[/code\]Likewise, with PDO I get to a similar place...\[code\] $stmt = $dbh->prepare("SELECT id FROM table WHERE email = ? "); if ($stmt->execute(array($_POST['email']))) { // what goes in here to loop through the rows?? // // something like this...? // while ($row = $stmt->fetch()) { echo $row['id']; } }\[/code\]
 
Back
Top