简体   繁体   中英

how do i print data in php

hi this is my code but its run without any results, i have a table "sliders " that contains id and url for image and i'm sure connect.php in fine. may you help me what is wrong?

<?php
    $sliders=array();
    include"connect.php";
    $query_getinfo="select * from sliders";
    $result=$connect->prepare($query_getinfo);
    $result->execute();

    while($row=$result->fetch(PDO:: FETCH_ASSOC)){
    $record=array();
    $record["id"]=$row["id"];
    $record["slide_url"]=$row["slide_url"];

    $slider[]=$record;

    //$sliders[]=$row["slide_url"];

    }

   print_r ($sliders);

?>

Looks like you do not have any parameters to pass so you can just replace prepare with query and remove execute, below are 2 examples

if you want to pass parameters

// prepare the query
$sliders = array();

$statement = $connect->prepare("select * from sliders WHERE type = ?");

// assuming type is string eg admin / user

$statement->bind_param("s", $_SESSION['type']);
// execute it
$statement->execute();

// ger result
$result = $statement->get_result();

// check if rows exists in table
if($result->num_rows > 0){

   while($row = $result->fetch_assoc()) {
     $record=array();
     $record["id"]=$row["id"];
     $record["slide_url"]=$row["slide_url"];
     $sliders[]=$record;
   }

   print "<pre";
   print_r($sliders);

}else{ 
  print "no sliders found";
}

$statement->close();

if you do not have parameters in query

$sliders = array();

// prepare the query

$result = $connect->query("select * from sliders");

// check if rows exists in table
if($result->num_rows > 0){

   while($row = mysqli_fetch_assoc($result)) {
     $record=array();
     $record["id"]=$row["id"];
     $record["slide_url"]=$row["slide_url"];
     $sliders[]=$record;
   }

   print "<pre";
   print_r($sliders);

}else{ 
  print "no sliders found";
}

// if you want to close connection here.
$connect->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM