最后一行这样print_r($res2);可以查询出数据来 为什么加上键名就会报错了?
print_r($res2['name']);
提示这个错误
Notice: Undefined index: name in D:\wamp\www\fenyechaxun.php on line 16
这是数据库的内容
<?php
header('content-type:text/html;charset=utf-8;');
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from test");
$stmt->execute();
$res=$stmt->fetchall();
$rows=count($res);
$pagesize=3;
$pagenum=ceil($rows/$pagesize);
$page=empty($_GET['page'])?1:$_GET['page'];
$startnum = ($page - 1)*$pagesize;
$query = "SELECT * FROM test LIMIT $startnum,$pagesize";
$stmt2=$pdo->prepare($query);
$stmt2->execute();
$res2=$stmt2->fetchall();
print_r($res2['name']);
?>
从你的截图可以看出~你查询出来的数组应该是这样子的:
所以你要拿'name'???不太可能吧??
一:要么你用$query = "SELECT name FROM test LIMIT $startnum,$pagesize";单独查询name;
二:可以用foreach循环
三:array_column($array,'key') 这个函数好像可以提取某一列的~你试试