dsue14118 2014-09-29 06:42
浏览 51
已采纳

不输出数据PDO


i'm still new to PDO stuff
i'm writing a pdo pagination but my problem is, its not outputting the data it just output blank white page

PHP CODE

include "config.php";
if(isset($_POST['checkin'])){

$country = $_POST['country'];
$city = $_POST['city'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];


$sql = $dbo->prepare("SELECT COUNT(id) FROM hotels");
if(!$sql->execute()){
    echo "No Count";
}else {
    $fetchrows = $sql->fetch();
    $rows = $fetchrows[0];
    $page_rows = 5;
    $last = ceil($rows/$page_rows);

    if($last < 1){
        $last = 1;
    }

    $page_num = 1;

    if(isset($_GET['pn'])){
       $page_num = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }

    $limit = ($page_num - 1) * $page_rows;


$sql = $dbo->prepare("SELECT * FROM hotels DESC WHERE h_country='$country' LIMIT $limit");
    echo "<h3>Total hotels found: $rows</h3>";
    echo "Page <b>$page_num</b> of <b>$last</b>";
    $hotels = $sql->fetchAll(PDO::FETCH_OBJ);
    foreach($hotels as $hotel){
        echo $hotel->h_title;
    }
  }

 }

i normally do while statement for pagination in a normal query, so i tried doing the same thing but in PDO, but it didn't work.
i dunno what's the problem.
It would be great if you can point out my mistake. Thanks

Here's my PDO Connection & Mysql, i copied the PDO connection code from a website.

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";

$dbserver = mysql_connect($dbhost,$dbuser,$dbpass);
if(!$dbserver) die ("Failed to Connect:" . mysql_error());

mysql_select_db($dbname) or die ("Unable to select the database");

try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);    
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

I'm not sure how the errors work

  • 写回答

2条回答 默认 最新

  • dongqindu8110 2014-09-29 08:33
    关注

    i still dunno what's the problem. i went to write a code that outputs all data,literally all NO LIMITS. and it work so i copied the code and paste it to the code above to see if it works and it did

    WORKING CODE:

    $stmt = $dbo->prepare("SELECT * FROM hotels WHERE h_country='Malaysia' LIMIT 5");
    $stmt->bindParam(':country', $country);
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    
    if(!$stmt->execute()){
           echo "
    PDO::errorInfo():
    ";
          print_r($dbo->errorInfo());
    }else {
        echo "<h3>Total hotels found: $rows</h3>";
    echo "Page <b>$page_num</b> of <b>$last</b>";            
    $hotels = $stmt->fetchAll(PDO::FETCH_OBJ);
    foreach($hotels as $hotel){
    
        echo "<h3>$hotel->h_title</h3>";
    }
    }
    

    i think maybe (just maybe) its the fault the (DESC)
    and i finally i know how to use while statements in PDO(was really easy but i didn't know :P)
    Thanks for the tips

    ORIGINAL CODE:

        $sql = $dbo->prepare("SELECT * FROM hotels DESC WHERE h_country='$country' LIMIT $limit");
        echo "<h3>Total hotels found: $rows</h3>";
        echo "Page <b>$page_num</b> of <b>$last</b>";
        $hotels = $sql->fetchAll(PDO::FETCH_OBJ);
        foreach($hotels as $hotel){
            echo $hotel->h_title;
        }
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据