<?php
try{
include_once "inc/db.inc.php" ;
include "inc/sessions.inc.php";
// Inserting into Database :
date_default_timezone_set('Asia/Amman');
$Current_time = time();
$DateTime = strftime("%d-%m-%Y/%H:%M",$Current_time);
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['btn'])){
if(!empty($_POST['name'])){
$admin = "Anonymous" ; //Default Admin
$Query = "INSERT INTO categories(datetime,creatorname,name)
VALUES(?,?,?)";
$Statement = $Connection->prepare($Query);
$Statement-> bindValue(1,$DateTime);
$Statement-> bindValue(2,$admin);
$Statement-> bindValue(3,$_POST['name']);
$Statement-> execute();
$_SESSION['SuccessMessage'] = "Successfully Added One New Category !" ;
} //if !empty(name)
else {
$_SESSION['ErrorMessage'] = "Please Fill The Feilds Correctly !" ;
}
} //if isset(btn)
else {
$_SESSION['ErrorMessage'] = "Sorry Something Went Wrong";
}
}
//Fetch out from the DB
$Fetch = "SELECT * FROM categories" ;
$Srno = 0;
$Statement2 = $Connection->prepare($Fetch);
$Statement2-> bindColumn('datetime',$DateTimeE);
$Statement2-> bindColumn('creatorname',$adminE);
//
//
//
//
//
//
// Getting Errors
$errorInfo = $Connection-> errorInfo();
if(isset($errorInfo[2])){
$error = $errorInfo[2];
}
}
catch (Exception $e) {
$error = $e-> getmessage();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/pub.css">
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<title>My Blog</title>
</head>
<body>
<?php
if(isset($error)) {
echo "<div class=\"alert alert-danger\">".
$error
."</div>" ;
}
else {
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<ul class="nav nav-pills nav-stacked" id="sideNav">
<br>
<li><a href="dashboard.php">Dashboard</a></li>
<li><a href="users.php"> Users</a></li>
<li><a href="compose.php">Compose</a></li>
<li class="active"><a href="categories.php">Categories</a></li>
<li><a href="comments.php">Comments</a></li>
<li><a href="index.php">Live Blog</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div> <!-- col-sm-2 -->
<div class="col-sm-10">
<h1>Categories :</h1>
<form method="post" action="categories.php">
<div class="form-group">
<div> <?php echo Message() ; echo SuccessMessage() ;?> </div>
<label for="name">Create New Category :</label>
<input type="text" id="name" class="form-control" placeholder="Category name" name="name"> <br>
<input id="btn" type="submit" name="btn" value="submit" class="btn btn-success btn-block">
</div>
</form>
<hr>
<!-- Fetching Out Some Data From The Database/ -->
<table class="table table-responsive table-stripped table-hover">
<tr>
<th>Sr.no</th>
<th>Created at</th>
<th>Creator</th>
<th>Name</th>
</tr>
<?php $Statement2->fetch(PDO::FETCH_BOUND);?>
<?php while($Statement2->fetch(PDO::FETCH_BOUND)){?>
<tr>
<td><?php echo $DateTimeE;?></td>
<td><?php echo $adminE;?></td>
</tr>
<?php }?>
</table>
</div> <!-- col-sm-10 -->
</div> <!-- row -->
</div> <!-- Container-fluid -->
<?php }?>
</body>
</html>
正如您在我的代码中看到的那样,我正在尝试从我的数据库中提取一些数据,但问题是。当我使用PDO的bindColumn()
方法时。从数据库中找不到任何东西。
抱歉我的英文
信息: - 我的数据库中有数据,它不是空的。
我应该做些什么,是我的代码中的问题如果陈述......等等或因为我错过了Code中的一些陈述,哦,实际上PDO很难理解我是从MySQL转换的PDO的语法
答案 0 :(得分:0)
你对bindColumn
的调用失败的原因是你没有执行第二个sql语句 - 即使你有两次调用fetch( PDO::FETCH_BOUND )
所以你会丢失显示的第一条记录
下面有一些评论 - 我用我自己的pdo连接测试了这个,这帮我解决了错误。我建议的一件事是在throw
处于各个关键点时使用try/catch
方法,这样您就可以根据变量是否丢失或查询失败等产生有意义的错误。
<?php
try{
$error = false;
include_once "inc/db.inc.php" ;
include "inc/sessions.inc.php";
/* These were not defined so threw errors when I ran the code */
/*
function Message(){
echo 'Message() called<br />';
}
function SuccessMessage(){
echo 'SuccessMessage() called<br />';
}
*/
date_default_timezone_set('Asia/Amman');
$Current_time = time();
$DateTime = strftime("%d-%m-%Y/%H:%M",$Current_time);
if( $_SERVER['REQUEST_METHOD'] === 'POST'){
if( isset( $_POST['btn'] ) ){
if( !empty( $_POST['name'] ) ){
$admin = "Anonymous";
/* datetime is a special word in sql - use backticks */
$sql = "insert into `categories` ( `datetime`,`creatorname`,`name` ) values (?,?,?)";
$stmt = $Connection->prepare( $sql );
/* You need to check the status of any call to `prepare` and use it to fork the code */
if( !$stmt )throw new Exception( sprintf('failed to prepare sql query: %s', implode( ',', $Connection->errorInfo() ) ) );
$stmt-> bindValue(1, $DateTime );
$stmt-> bindValue(2, $admin );
$stmt-> bindValue(3, $_POST['name'] );
$stmt-> execute();
$_SESSION['SuccessMessage'] = "Successfully Added One New Category !" ;
} else {
$_SESSION['ErrorMessage'] = "Please Fill The Feilds Correctly !" ;
}
} else {
$_SESSION['ErrorMessage'] = "Sorry Something Went Wrong";
}
}//close POST processing
/* You only need two columns */
$sql = "select `datetime`,`creatorname` from `categories`";
$Srno = 0;
$stmt = $Connection->prepare( $sql );
$stmt-> bindColumn( 'datetime', $DateTimeE );
$stmt-> bindColumn( 'creatorname', $adminE );
/* The reason calls to `bindColumn` were not working was you failed to execute the query */
$results=$stmt->execute();
if( !$results ) throw new Exception( sprintf('Query failed: %s', implode( ',', $stmt->errorInfo() ) ) );
$errorInfo = $Connection->errorInfo();
if( isset( $errorInfo[2] ) ){
$error = $errorInfo[2];
}
} catch ( Exception $e ) {
$error = $e-> getmessage();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/pub.css">
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<title>My Blog</title>
</head>
<body>
<?php
if( !empty( $error ) ) {
echo "<div class='alert alert-danger'>$error</div>" ;
} else {
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<ul class="nav nav-pills nav-stacked" id="sideNav">
<br>
<li><a href="dashboard.php">Dashboard</a></li>
<li><a href="users.php"> Users</a></li>
<li><a href="compose.php">Compose</a></li>
<li class="active"><a href="categories.php">Categories</a></li>
<li><a href="comments.php">Comments</a></li>
<li><a href="index.php">Live Blog</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div><!-- col-sm-2 -->
<div class="col-sm-10">
<h1>Categories :</h1>
<form method="post" action="categories.php">
<div class="form-group">
<div>
<?php
echo Message();
echo SuccessMessage();
?>
</div>
<label for="name">Create New Category :</label>
<input type="text" id="name" class="form-control" placeholder="Category name" name="name"> <br>
<input id="btn" type="submit" name="btn" value="submit" class="btn btn-success btn-block">
</div>
</form>
<hr>
<table class="table table-responsive table-stripped table-hover">
<tr>
<th>Sr.no</th>
<th>Created at</th>
<th>Creator</th>
<th>Name</th>
</tr>
<?php
/* only one Fetch command in the loop - otherwise you effectively lose the first record */
while( $stmt->fetch( PDO::FETCH_BOUND ) ){
?>
<tr>
<td><?php echo $DateTimeE;?></td>
<td><?php echo $adminE;?></td>
</tr>
<?php
}//close while loop
?>
</table>
</div> <!-- col-sm-10 -->
</div> <!-- row -->
</div> <!-- Container-fluid -->
<?php
}//close if/else
?>
</body>
</html>
数据库内容
+------------------+-----------------+--------+
| datetime | creatorname | name |
+------------------+-----------------+--------+
| 23/12/2018 10:50 | jimbob | jimmy |
| 24/12/2018 16:32 | Freda | Freda |
| 25/12/2018 15.25 | Geronimo | Gogs |
| 26/12/2018 14:41 | Susan Hampshire | Susan |
| 27/12/2018 | Harriet | Harpy |
| 23/12/2018 10:50 | jimbobx | jimmyx |
+------------------+-----------------+--------+
结果