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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动