怎么样获取一行数据呢和所有的数据的行数呢?

WBOY
Release: 2016-06-06 20:20:30
Original
1287 people have browsed it

我加上了 $myrow = mysql_fetch_row($res);这一句想获取一行数据 为什么获取不到一行 提示这个 ( ! ) Warning: mysql_fetch_row() expects parameter 1 to be resource, array given in D:\wamp\www\chaxun.php on line 7

<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
 $stmt=$pdo->prepare("select * from test");
 $stmt->execute();
 //$myrow = mysql_fetch_row($stmt->execute());
 $res=$stmt->fetch();
 $myrow = mysql_fetch_row($res);
 print_r($myrow);
?>
</code>
Copy after login
Copy after login

回复内容:

我加上了 $myrow = mysql_fetch_row($res);这一句想获取一行数据 为什么获取不到一行 提示这个 ( ! ) Warning: mysql_fetch_row() expects parameter 1 to be resource, array given in D:\wamp\www\chaxun.php on line 7

<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
 $stmt=$pdo->prepare("select * from test");
 $stmt->execute();
 //$myrow = mysql_fetch_row($stmt->execute());
 $res=$stmt->fetch();
 $myrow = mysql_fetch_row($res);
 print_r($myrow);
?>
</code>
Copy after login
Copy after login

PDOStatement::rowCount — 返回受上一个 SQL 语句影响的行数

PDOStatement::fetch — 从结果集中获取下一行

<code><?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', 'root');

$query = $pdo->query('SELECT * from users');
//获取一行
$result=$query->fetch();

print_r($result);

$total = $pdo->query('SELECT * from test');
//统计条数
$count=$total->rowCount();

echo $count;</code>
Copy after login

要用while循环取吧

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!