1

I just learned php to display information on the page, I got an error, please help me .. thanks

this 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>";

?>

I followed the book and this is the original script:

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

this Output

Qirel
  • 25,449
  • 7
  • 45
  • 62

2 Answers2

1

In the following lines

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

..you are trying to fetch the count, but treating the resulting query-object as the results you want. You'll need to fetch the data before you can use it, as such

$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);

Note the fetch() and that the $total_result_fetched variable now is an array.

You are also exposed to SQL injections, because you use variables directly in the query. You should use prepared statements to protect yourself from that. Table and column names cannot be bound, so those should at least be whitelisted. Using prepare() and execute(), you can bind the values as shown below

$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);

Consult the following topics and documentation

Qirel
  • 25,449
  • 7
  • 45
  • 62
0
//FIX ERROR:  from error object of class PDOStatement could not be converted to number. 

Try This:

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

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

$total_laman  = ceil($num ['Num'] / $per_laman);
Manoj Vadehra
  • 836
  • 4
  • 17