I try to run
// 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` ,'Facebook', '')";
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,'Twitter', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
but only Twitter will be replaced. I'm new in PHP. Maybe someone could help me to fix this. Thank you.
The $conn->query($sql)
statement runs your query. You are overwriting the first $sql
(the one with Facebook) before you run it. You'll wan to do something like
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,'Facebook', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully with Facebook";
} else {
echo "Error updating record: " . $conn->error;
}
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,'Twitter', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully with Twitter";
} else {
echo "Error updating record: " . $conn->error;
}
Your first query won't be executed because the $sql string will be overwritten, and you end up executing only the second one.
Since you are executing the same query more than once, only changing a parameter, I would suggest you to use prepared statements and placeholders, something like this:
$sql = "UPDATE pm_videos SET `description` = REPLACE(`description`, ?, '')";
$conn = new mysqli($servername,$username, $password, $dbname);
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $content);
$content='facebook';
$stmt->execute();
$content='twitter';
$stmt->execute();
$stmt->close();
$conn->close();
(I removed all error checks, you might want to add your own error checking routine).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.