我最近开始使用PDO,之前我使用过Mysql函数。现在我正在尝试通过上一个网页中的选定ID获取并显示记录。 我很少使用Connexion Class
Class Connexion {
private $host = "localhost";
private $user = "root";
private $pass = "";
private $dbname = "cvtheque";
private $dbh;
private $stmt;
public function __construct() {
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch (PDOException $e) {
echo "Connexion Error: " . $e->getMessage();
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() {
return $this->stmt->execute();
}
public function resultset() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount() {
return $this->stmt->rowCount();
}
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
public function beginTransaction() {
return $this->dbh->beginTransaction();
}
public function endTransaction() {
return $this->dbh->commit();
}
public function cancelTransaction() {
return $this->dbh->rollBack();
}
public function debugDumpParams() {
return $this->stmt->debugDumpParams();
}
}
我使用这些脚本来显示数据库中的项目
<?php
$c = new Connexion();
$c->query("select * from annonce order by 'client' DESC Limit 20");
$rows = $c->resultset(); ?>
<div class="Joblist" charset="UTF-8">
<?php foreach($rows as $r) { ?>
<p class="Title">
<span class="Ref"></span>
<a href="details.php?id=<?php echo($r['id_annonce']) ?>">
<?php echo($r['poste']); ?></a></p>
<p class="Date"><span class="Fright"><?php echo($r['type_contrat']); ?></span>
<span><a href="http://www.maximus-it.com/job-search-result.php?sector=1#" class="Sectorbtn"><?php echo($r['client']); ?></a></span></p>
<p style="text-align:justify;"><?php echo tronque($r['desc_annonce'], 230);?></p>
<p class="More"><a href="details.php?id=<?php echo($r['id_annonce']) ?>">Voir plus</a> </p>
<?php }?>
</div>
然后我使用这些方法按ID从URL
中选择行 <?php
require_once '../Model/Connexion.php';
$c = new Connexion();
if (isset($_GET['code'])) {
$results = $c->query("select * from annonce where id=".$_GET['code']);
$row = $c->single();
}
?>
<div class="Content FCKeditor">
<a style="float:right" href="javascript:history.go(-1);">back</a>
<?php while($row) { ?>
<h1><?php echo($r['poste']); ?></h1>
<div class="Jobdetails">
<p class="Date"><span></span> |
<span><a href="job-details.php?id=792" class="Sectorbtn"><?php echo($r['client']); ?></a></span> |
<span><?php echo($r['type_contrat']); ?></span>
</p>
<?php echo($r['desc_annonce']); ?>
<?php }?>
</div>
</div>
它显示&#34;未定义的变量:$ row&#34;在这一行,怎么解决这个问题?
答案 0 :(得分:0)
此single()
功能不正确
public function single() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
应该是
public function single() {
$this->stmt->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
同样适用于resultset()
:
public function resultset() {
$this->stmt->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
除了getter和setter之外,大多数这些函数都是一个无意义的内容,但实际情况并非如此,如果需要可重用性,可以用构造函数逻辑创建一个函数
public function get_connexion() {
return $this->dbh;
}
php
<?php
if (isset($_GET['code'])) {
//require only when code is set
require_once '../Model/Connexion.php';
//create conn
$db = new Connexion();
$conn = $db->get_connexion();
$stmt = $conn->prepare('select * from annonce where id = ?');
$stmt->execute(array($_GET['code']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
HTML (自单曲以来没有循环)
<div class="Content FCKeditor">
<a style="float:right" href="javascript:history.go(-1);">back</a>
<h1><?php echo($row['poste']); ?></h1>
<div class="Jobdetails">
<p class="Date"><span></span> |
<span><a href="job-details.php?id=792" class="Sectorbtn"><?php echo($row['client']); ?></a></span> |
<span><?php echo($row['type_contrat']); ?></span>
</p>
<?php echo($row['desc_annonce']); ?>
</div>
</div>
避免使用Sql注入:
<?php
require_once '../Model/Connexion.php';
$c = new Connexion();
$c->query('select * from annonce where id_annonce= :id');
$c->bind(':id', $_GET['id']);
$r = $c->single();
?>