用于PDO的Mysql_data_seek?

时间:2014-04-27 16:00:29

标签: php mysql pdo

我试图将我的所有php遗留代码移出 PDO Statments 。一切似乎都很有效,但我通常在这种情况下使用mysql_data_seek函数,我试图找到PDO的方法,但老实说,我找不到与此相关的任何内容。这就是我实际做的(没有PDO)

$query = mysql_query("SELECT * sometable");
$array = mysql_fetch_assoc($query);

$name = $array['name']

// $name is returned well, but now I want to loop it, so I use mysql_data_seek to reset the query. I want just one row, not more.

mysql_data_seek($query, 0);

// now i can loop

while($arr = mysql_fetch_assoc($array)){
    echo "name: " . $arr[$arr];
}

所以现在我尝试做这样的事情:

$query = $conn->query("SELECT * sometable");
$array = $query->fetch();

$name = $array['name'];

// It outputs just one row, that's what I really want to, so I want to loop it now.

foreach($query->fetchAll() as $loop){
    echo $loop['name']
}

// This time I don't get anything...

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

两次获取相同的数据是没有意义的 你用mysqli做错了,你不应该用pdo重复它。

$query = $conn->query("SELECT * sometable");
$array = $query->fetchAll();
foreach($array as $row){
    echo $row['name'];
}