【问题标题】:php decode special entities in mysql [closed]php解码mysql中的特殊实体[关闭]
【发布时间】:2014-10-06 15:36:37
【问题描述】:

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

所以,这是我尝试过的:

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

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

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

但我没有打印输出..

有人可以帮我解决吗?

【问题讨论】:

  • 如您所见,$query 尚未定义。所以它没有“执行”功能。也许您应该尝试在$conn$stmt 上调用它。

标签: php pdo decode html-entities


【解决方案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']
                );
}

【讨论】:

  • 感谢您的完整解释。那么,$results 返回一个数组,其中包含我在 fetch 中指定的列?我应该写 while ($results = $stmt->fetch(columnName)) 吗?对不起,如果我的问题真的是初学者:(
  • 非常感谢!它工作得很好,还有一个问题:如果我想用解码的行更新我的数据库中的所有行,如果我将 var_dump($result) 行放在 while 循环中,然后将转储数据插入到一个新表还是用 $result[columnName] 更新表更好?
  • 我不确定你的意思是什么?你想用编码的值替换这些值吗? var_dump 只是为了调试转储数据
  • 是的,我的意思是如果我想用编码的值更新(替换)值,我可以在 csv 转储中获得输出还是用新值更新表更好?
  • @mOna 我在我的答案上花费了,但请确保你测试它,我只是在记事本中快速输入它,所以确保它有效
【解决方案2】:

您没有定义$query,因此它没有execute() 功能。如果你想执行你准备好的语句,你应该调用$stmt-&gt;execute()

【讨论】:

  • 谢谢,是的,你是对的,所以我编辑了我的问题,但仍然没有打印输出..(虽然之前的错误已解决)
猜你喜欢
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
相关资源
最近更新 更多