Jonathan
Jonathan

Reputation: 113

Not outputting data 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

Upvotes: 0

Views: 75

Answers (2)

Jonathan
Jonathan

Reputation: 113

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 "\nPDO::errorInfo():\n";
      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;
    }
  }

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

You are missing execute() in your second query.

$sql = $dbo->prepare("SELECT * FROM hotels DESC WHERE h_country='$country' LIMI$limit");

// add this
$sql->execute();

echo "<h3>Total hotels found: $rows</h3>";
echo "Page <b>$page_num</b> of <b>$last</b>";
$hotels = $sql->fetchAll(PDO::FETCH_OBJ);

Upvotes: 1

Related Questions