0
所以我的数据库中有很多有问题的值。 有两列,一个是电子邮件。有些电子邮件是以这种方式存储的“[email protected],在起始处的引号是无用的。我希望将其转换为[email protected]。 同样,还有另一个领域称为公司,其中有像这样的数据stackoverflow“,末尾的引号也需要被删除。从字符串中删除多余的引号
我写了这段代码,但由于某些原因,这并不删除引号。 我在做什么错? 欢迎任何建议。
global $conn;
try
{
$statement= $conn->query("SELECT * from emails");
$statement->setFetchMode(PDO::FETCH_ASSOC);
while($rows = $statement->fetch())
{
$email=$rows['emailid'];
$company=$rows['company'];
$len = strlen($company);
if($email{0} == '"')
{
//problem at start
//fix there.
$updated = substr($email, 1);
//now update
try
{
$statement = $conn->prepare("UPDATE emails set emailid = :email where indexid = :indexid");
$statement->bindParam(':email',$updated);
$statement->bindParam(':indexid',$row['indexid']);
$statement->execute();
$statement->closeCursor();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
elseif($company{$len-1} == '"')
{
$updated = substr($company, 0, -1);
//now update
try
{
$statement = $conn->prepare("UPDATE emails set company = :company where indexid = :indexid");
$statement->bindParam(':company',$updated);
$statement->bindParam(':indexid',$row['indexid']);
$statement->execute();
$statement->closeCursor();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{
echo "";
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
为什么不'str_replace'? –
你在if语句中返回true;它只会运行一次,因为返回会终止循环。如果这不是问题 - 请添加一些调试。 SQL语句是否正在运行?它是否选取正确的条目?你的substr调用是否工作?找出哪个部分不工作。 – andrewsi
你可以删除不必要的字符串一次在数据库中使用MySQL'替换'功能 –