0

I am practicing using PDO fetch methods to retrieve data from the table. I would like to a counter in a while loop to retrieve data one row at a time. Please give me some advise how to accomplish this. Thanks!

Here are my 2 code samples using PDO::Query() and PDO::fetch() method. code sample 1 using PDO::Query() Method

$sql = 'select first_name, last_name, pd, b_month, b_day, b_year from reg_data';
$birth_date = '';
try
{
    foreach($con->query($sql) as $row)
    {
       print $row['first_name'] . "  ";
       print $row['last_name']. "  ";
       print $row['pd'] . "  ";
       $birth_date =  $row['b_month'] . "-". $row['b_day'] . "-". $row['b_year'];
       print "$birth_date";
    } 

}
catch(PDOException $e)
{
    echo " There is a problem with you db connection";
    echo $e->getMessage();
}

sample 2 using PDO::fetch() method

try {
    $con = new PDO ($dns, $db_uid, $db_pd, $option);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "select * from reg_data";
    $stmt = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
    $stmt->execute();

    //using cursor to interate through array
    while($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT))
    {
        $data = $row[0].$row[1]. $row[2] .$row[3].$row[4].$row[5];
        print $data;
    }
   $stmt = null; //close the handle
}
catch(PDOException $e)
{
    echo " There is a problem with you db connection";
    print $get->getMessage();
}
3
  • 1
    So... what is your question? Commented Mar 5, 2013 at 13:26
  • 1
    I think this is the question.. but I can't make heads or tails of it: "I would like to a counter in a while loop to retrieve data one row at a time" Commented Mar 5, 2013 at 13:31
  • 1
    please refer to the tag wiki for the proper code example Commented Mar 5, 2013 at 13:32

2 Answers 2

2

I think you need PDO::fetchAll.
A code example straight from the tag wiki:

//connect
$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'root','', $opt);

//retrieval
$stm = $pdo->prepare("select * from reg_data");
$stm->execute();
$data = $stm->fetchAll();
$cnt  = count($data); //in case you need to count all rows
//output
?>
<table>
<? foreach ($data as $i => $row): ?>
  <tr>
    <td><?=$i+1?></td> <!-- in case you need a counter for each row -->
    <td><?=htmlspecialchars($row['first_name'])?></td>
  </tr>
<? endforeach ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a variable and increment it in the loop

$counter++ ;

1 Comment

Or just fetch everything into array and count the array, which is 2 lines of code and requires no loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.