MySQL通过PHP更新

时间:2016-01-02 08:18:26

标签: php mysql mysqli sql-update

我尝试更新SQL表,但我的代码不起作用。也许有人可以看一下。

<?php
$servername = "localhost";
$username = "user";
$password = "pwd";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,  "Instagram:",  "" ";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

我收到500错误。

当我在数据库中直接执行它时,效果非常好:

UPDATE pm_videos 
SET `description` = REPLACE(`description`, "Instagram:",  "");

5 个答案:

答案 0 :(得分:2)

$which python -a
/usr/local/bin/python

您的代码中有一些语法错误尝试上面的

答案 1 :(得分:1)

你搞砸了引号问题。这会改变

$sql = "UPDATE pm_videos SET  `description` = REPLACE(  `description` ,  'Instagram:',  '' )";

答案 2 :(得分:1)

这是因为当您在另一个$this->email->set_mailtype("html"); 中包含"时,PHP假设您正在关闭语句。要解决此问题,只需在双引号内包含单引号,例如:

"

答案 3 :(得分:1)

那是因为你错误地连接了字符串“Instagram”:和“”。

试试这段代码:

 $sql = 'UPDATE pm_videos SET  `description` = REPLACE(  `description` ,  "Instagram:",  ""); ';

答案 4 :(得分:1)

请使用单引号或双引号:

$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,  'Instagram:',  '' ";