如何修复注意:类PDOStatement的对象无法转换为int

时间:2017-08-27 12:04:10

标签: php mysql database pdo

我刚刚学会了php在页面上显示信息,我收到了一个错误,请帮帮我..谢谢

此filepolitik.php

<?php
  include 'koneksi.php';

  $tabel1 = 'isiberita';
  $tabel2 = 'kategori';
  $id=1;

  $hal = $_GET['hal']; //*Error : Notice: Undefined index: hal ???
  //validasi halaman
  if (!isset($_GET['hal'])) {
          $page = 1;
  } else {
    $page = $_GET['hal'];
  }
  //data tampil perhalaman
  $max_result = 2;

  $from = (($page * $max_result) - $max_result);

  //query tampil perhalaman
  $sql=$conn->query("SELECT * FROM  $tabel1,$tabel2 where $tabel1.id_kategori=$id and $tabel1.id_kategori=$tabel2.id_kategori
                    ORDER BY tanggal DESC LIMIT $from,$max_result");
while ($tampil = $sql->fetch(PDO::FETCH_ASSOC))
{

      $data = substr($tampil['isi'],0,200);
      echo "<div class='box'>";
      echo "<p align='justify'>";
      echo "<img class='gambar' src='$tampil[gambar]' width=150px height=150px align='left' />";
      echo "<font valign='top'>";
      echo "<strong>";
      echo $tampil['judul'];
      echo "</strong>";
      echo $data;

      echo "<a href='index.php?menu=detail_politik&id==$tampil[id_berita]'>  baca lengkap>>>></a>";
      echo "</font></p></div><br>";
}

//total
$total_result = $conn->query("SELECT count(*) as Num From $tabel1 where $tabel1.id_kategori=$id");
//Notice: Object of class PDOStatement could not be converted to int ??
$total_pages = ceil($total_result / $max_result);

echo "<center> piliih Halaman <br />";
//proses link
if ($hal > 1) {
  $prev = ($page - 1);
  echo "<a href=$_SERVER[PHP_SELF]?menu=politik&hal$prev>
        <-Sebelumnya </a>";
}

//link daftar urutan Halaman
for ($i=1; $i <= $total_pages; $i++) {
        if (($hal)==$i) {
          echo "$i";
        } else {
          echo "<a href=$_SERVER[PHP_SELF]?menu=politik&hal=$i>$i</a>";
        }
      }
        //link halaman selanjutnya
        if ($hal < $total_pages) {
          $next = ($page + 1);
          echo "<a href=$_SERVER[PHP_SELF]?menu=politik&hal=$next>Selanjutnya-></a>";
        }

      echo "</center>";

?>

我跟着这本书,这是原始剧本:

$total_result = mysql_result(mysql_query("SELECT count(*) as Num From $tabel1 where $tabel1.id_kategori=$id"),0);

this Output

2 个答案:

答案 0 :(得分:1)

以下几行

$total_result = $conn->query("SELECT count(*) as Num From $tabel1 where $tabel1.id_kategori=$id");
$total_pages = ceil($total_result / $max_result);

..您正在尝试获取计数,但将生成的查询对象视为您想要的结果。您可以在使用之前获取数据,因此

$total_result = $conn->query("SELECT count(*) as Num From $tabel1 where $tabel1.id_kategori=$id");
$total_result_fetched = $total_result->fetch(PDO::FETCH_ASSOC);
$total_pages = ceil($total_result_fetched['Num'] / $max_result);

注意fetch()并且$total_result_fetched变量现在是一个数组。

您也会接触到SQL注入,因为您在查询中直接使用变量。您应该使用准备好的声明来保护自己。表和列名称不能绑定,因此至少应将其列入白名单。使用prepare()execute(),您可以绑定值,如下所示

$total_result = $conn->prepare("SELECT count(*) as Num From $tabel1 where $tabel1.id_kategori=:id");
$total_result->execute(["id" => $id]);
$total_result_fetched = $total_result->fetch(PDO::FETCH_ASSOC);
$total_pages = ceil($total_result_fetched['Num'] / $max_result);

请参阅以下主题和文档

答案 1 :(得分:0)

//FIX ERROR:  from error object of class PDOStatement could not be converted to number. 

尝试一下:

$query = $connection->query("SELECT count(*) as Num From tbl_blog");

$num   = $query->fetch(PDO::FETCH_ASSOC);

$total_laman  = ceil($num ['Num'] / $per_laman);