Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What is the equivalent of these two code in PDO

first:

  $row=mysql_fetch_array($query);

second:

 while($row=mysql_fetch_array($query)){
   $data[]=$row;
 }

i used these codes below but they are not exact same i guess, because the rest of the code didn't work.

$row = $query->fetch(PDO::FETCH_NUM);

and

 $data[] = $query->fetch(PDO::FETCH_ASSOC);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
457 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here are the correspondences:

  • mysql_fetch_array = fetch(PDO::FETCH_BOTH) - The rows are arrays with both numeric and named indexes.
  • mysql_fetch_assoc = fetch(PDO::FETCH_ASSOC) - The rows are arrays with named indexes.
  • mysql_fetch_row = fetch(PDO::FETCH_NUM) - The rows are arrays with numeric indexes.
  • mysql_fetch_object = fetch(PDO::FETCH_OBJ) or fetch(PDO::FETCH_CLASS) depending on whether you specify the optional className argument to mysql_fetch_object. The rows are objects, either of the specified class or stdClass.

The while loop is equivalent to:

$data = $query->fetchAll(PDO::FETCH_BOTH)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...