php解码mysql中的特殊实体

时间:2014-10-06 15:36:38

标签: php pdo decode html-entities

我是PDO的新手,我正在尝试解码我​​的表“test”中的所有行,其中包含特殊实体,例如“('L& eacute; on:The Professional')”而不是“Léon:The专业的”。

所以,这是我试过的:

<?php
require_once('connection.php');

$stmt = $conn->prepare("SELECT * from test");
$stmt->execute();
while ($results = $stmt->fetch()){

 $b = html_entity_decode($stmt);
 echo $b;
 }

但我没有打印输出..

有人可以帮我解决一下吗?

2 个答案:

答案 0 :(得分:1)

您没有定义$query,因此它没有execute()功能。如果您希望执行准备好的陈述,请致电$stmt->execute()

答案 1 :(得分:1)

  • prepare()返回一个语句对象(在您的情况下为$stmt
  • fetch()返回关联数组,其中索引是列名

$sql = "SELECT column1, column2, column3 from test";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = array()
while ($row = $stmt->fetch()){
   $resutlt[] = array('column1' => html_entity_decode($row['column1']),
                      'column2' => html_entity_decode($row['column2']),
                      'column3' => html_entity_decode($row['column3'])
                );
}
var_dump($result);
return $result;

<小时/> 编辑:替换值

//prepare select
$sql = "SELECT id, column1, column2, column3 from test";
$stmt = $conn->prepare($sql);
$stmt->execute();

//prepare update
$update_sql = "UPDATE test SET column1=?,column2=?,column3=? WHERE id = ?;";
$update_stmt = $conn->prepare($update_sql);

while ($row = $stmt->fetch()){
//update
$update_stmt->execute(array(html_entity_decode($row['column1']),
                            html_entity_decode($row['column2']),
                            html_entity_decode($row['column3']),
                            $row['id']
                );
}