不输出数据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
i还是PDO的新东西
i'm写一个pdo分页但是 我的问题是,它没有输出数据\只输出空白页 p>
PHP CODE p>
include“config.php”;
if(isset($ _ POST ['checkin'])){
$ country = $ _POST ['country'];
$ city = $ _POST ['city'];
$ checkin = $ _POST ['checkin'];
$ checkout = $ _POST ['checkout'];
$ sql = $ dbo-&gt; prepare(“SELECT COUNT(id)FROM hotels”);
if(! $ sql-&gt; execute()){
echo“No Count”;
} else {
$ fetchrows = $ sql-&gt; fetch();
$ rows = $ fetchrows [0];
$ page_rows = 5;
$ last = ceil($ rows / $ page_rows);
if if($ last&lt; 1){
$ last = 1;
}
$ page_num = 1 ;
if(isset($ _ GET ['pn'])){
$ page_num = preg_replace('#[^ 0-9]#','',$ _GET ['pn']); \ n}
$ limit =($ page_num - 1)* $ page_rows;
$ sql = $ dbo-&gt; prepare(“SELECT * FROM hotels DESC WHERE h_country ='$ country'LIMIT $ 限制“);
echo”&lt; h3&gt;总热点 els发现:$ rows&lt; / h3&gt;“;
echo”Page&lt; b&gt; $ page_num&lt; / b&gt; of&lt; b&gt; $ last&lt; / b&gt;“;
$ hotels = $ sql-&gt; fetchAll(PDO :: FETCH_OBJ);
foreach($ hotels as $ hotel){
echo $ hotel-&gt; h_title;
}
}
}
code> pre>
我通常在普通查询中为分页做while语句,所以我尝试做同样的事情但是 在PDO中,但它没有用。
i dunno有什么问题。
如果你能指出我的错误那就太好了。
谢谢 p>
这是我的 PDO Connection&amp; Mysql,我从网站复制了PDO连接代码。 p>
$ dbhost =“localhost”;
$ dbuser =“root”;
$ dbpass =“”;
$ dbname =“test”;
$ dbserver = mysql_connect($ dbhost,$ dbuser,$ dbpass);
if(!$ dbserver)die(“无法连接:”。mysql_error( );
mysql_select_db($ dbname)或die(“无法选择数据库”);
try {
$ dbo = new PDO('mysql:host = localhost; dbname ='。$ dbname, $ dbuser,$ dbpass);
} catch(PDOException $ e){
print“错误!:”。$ e-&gt; getMessage()。“&lt; br /&gt;”;
die();
}
code> pre>
我不是 确定错误是如何工作的 p>
div>
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;
}
}
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);