Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用pdo获取所有行?_Php_Mysql_Pdo_Fetchall - Fatal编程技术网

Php 如何使用pdo获取所有行?

Php 如何使用pdo获取所有行?,php,mysql,pdo,fetchall,Php,Mysql,Pdo,Fetchall,我正在制作一个博客,我想使用pdo语句获取所有行,但无论我做什么,只有一行返回,即使我的数据库中有两行 下面是我连接的代码示例: <?php try{ $link=new PDO('mysql:host=127.0.0.1;dbname=blog1','root',''); $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ echo $e-&g

我正在制作一个博客,我想使用pdo语句获取所有行,但无论我做什么,只有一行返回,即使我的数据库中有两行

下面是我连接的代码示例:

<?php
try{
$link=new PDO('mysql:host=127.0.0.1;dbname=blog1','root','');
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);




 } catch(PDOException $e){
 echo $e->getMessage();
   die();
  }
?>

然后我尝试获取所有行

<?php
   require 'Escape.php';
  $posts_query=$link->query('SELECT * FROM posts');
  $posts_query->execute();
/* variable that holds a with the database link and query in it then fetches 
  all the data related to the query into and assoc array */
$result=$posts_query->fetchAll();

 //counting all rows
 $count=$posts_query->rowCount();
  if($count>0){



 foreach($result as $r){
      $id= $r['id'];
    $title= $r['title'] ;
   $content=$r['content'];
       $date= $r['date'];

//admin buttons
 $admin="";
//keeping title safe
    $Title=htmlentities($title);
    //keeping output safe
    $output=htmlentities($content);
 // styling the posts to be echoed with secure variables
$posts= "<div><h2><a href='view_post.php?pid=$id' class='names'>$Title</a> 
  </h2><h3>$date</h3><p>$output</p>$admin</div>";
  escape($posts);
   }
 echo"<div id=posts>$posts</div>";
  } else{
echo 'There are no posts to display.';
  }

    ?>

循环时,
$posts
的值将重置为最新的行,或者使用concat
操作符附加每个post的值:

if($count>0){
 $posts = "";
 foreach($result as $r){
    // Define your variable
    $posts .= "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
    escape($posts);
   }
  echo"<div id=posts>$posts</div>";
} else { ... }
if($count>0){
$posts=“”;
foreach(结果为$r){
//定义变量
$posts.=“$date$output

$admin”; 逃逸(员额); } 回音“$posts”; }否则{…}
或在循环时打印每篇文章:

if($count>0){
 $posts = "";
 echo "<div id='posts'>";
 foreach($result as $r){
    // Define your variable
    $posts = "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
    escape($posts);
    echo $posts;
   }
  echo"</div>";
} else { ... }
if($count>0){
$posts=“”;
回声“;
foreach(结果为$r){
//定义变量
$posts=“$date$output

$admin”; 逃逸(员额); echo$员额; } 回声“; }否则{…}
看起来您在每次迭代中都会重新分配帖子,请尝试将这些值添加到数组中,并在希望
回送这些值时对其进行内爆

  require 'Escape.php';
  $posts_query=$link->query('SELECT * FROM posts');
  $posts_query->execute();

  $result=$posts_query->fetchAll();

  $count=$posts_query->rowCount();
  if($count>0){
    $posts=[];
    foreach($result as $r){
      $id= $r['id'];
      $title= $r['title'] ;
      $content=$r['content'];
      $date= $r['date'];
      $admin="";
      $Title=htmlentities($title);
      $output=htmlentities($content);
      // Add the HTML to the $posts array, also taking advantage of NOWDOCS
      $posts[]= <<< HTML
<div>
  <h2>
    <a href='view_post.php?pid={$id}' class='names'>{$Title}</a> 
  </h2>
  <h3>{$date}</h3>
  <p>{$output}</p>
  {$admin}
</div>
HTML;
      escape($posts);
   }
   echo"<div id='posts'>" . implode('', $posts) . "</div>";
 } else {
   echo 'There are no posts to display.';
}
需要“Escape.php”;
$posts_query=$link->query('SELECT*FROM posts');
$posts_query->execute();
$result=$posts_query->fetchAll();
$count=$posts\u query->rowCount();
如果($count>0){
$员额=[];
foreach(结果为$r){
$id=$r['id'];
$title=$r['title'];
$content=$r['content'];
$date=$r['date'];
$admin=“”;
$Title=htmlentities($Title);
$output=htmlentities($content);
//将HTML添加到$posts数组中,同时利用NOWDOCS

$posts[]=每次循环时,您都会将
$posts
重新分配给最新的帖子,而不是添加它。在循环中移动
回显“$posts”
。非常感谢。这对我很有帮助!❤别担心,伙计