dongzhangnong2063 2015-05-07 20:41
浏览 37
已采纳

MySQL Update SET替换为PHP多个查询

I'm trying to run 3 lines of the same code as seen below.I've also tried the W3Schools PHP mysqli_multi_query() Function but it gives me a syntax error and only the 1-st line of code works the ExistingWord1

I also read a couple of stackoverflow posts about this and this can't figure it out

<?php
include 'words.php';
include('../config.php');
$servername = DB_HOSTNAME;
$username = DB_USERNAME;
$password = DB_PASSWORD;
$dbname = DB_DATABASE;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
/*UTF-8 format*/
$conn->set_charset("utf8");

$sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord1', '$ReplacerWord1') WHERE name LIKE '%$SearchWord1%';";
$sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord2', '$ReplacerWord2') WHERE name LIKE '%$SearchWord2%';";
$sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord3', '$ReplacerWord3') WHERE name LIKE '%$SearchWord3%';";

if ($conn->query($sql) === TRUE) {
    echo "Products updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

This is the full Code that i am trying to run as by itself it only runs the 1st $sql =""

and ignores the other 2

as for the variables in the REPLACE

$ExistingWord1 words that are in the database name
$ReplacerWord1  replaces the ExistingWord1
'%$SearchWord1%' real database names

Thanks for the help guys

Everyone in this post was really helpful i really do appriciate it.

  • 写回答

3条回答 默认 最新

  • duankuang1046 2015-05-07 20:52
    关注

    According to the fragment provided only last $sql value will be executed, since you redefine same variable 3 times, only the last value stored in var $sql.

    To fix that you can try:

    $sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord1', '$ReplacerWord1') WHERE name LIKE '%$SearchWord1%';";
    $sql .= "UPDATE product_description SET name = REPLACE(name, '$ExistingWord2', '$ReplacerWord2') WHERE name LIKE '%$SearchWord2%';";
    $sql .= "UPDATE product_description SET name = REPLACE(name, '$ExistingWord3', '$ReplacerWord3') WHERE name LIKE '%$SearchWord3%';";
    

    UPDATE So now you have just debug your query and code any preffered way. For example:

     $sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord1', '$ReplacerWord1') WHERE name LIKE '%$SearchWord1%';";
    if ($conn->query($sql) === TRUE) {
        echo "1. Products updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
        $sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord2', '$ReplacerWord2') WHERE name LIKE '%$SearchWord2%';";
    if ($conn->query($sql) === TRUE) {
        echo "2. Products updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    
        $sql = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord3', '$ReplacerWord3') WHERE name LIKE '%$SearchWord3%';";
    
    if ($conn->query($sql) === TRUE) {
        echo "3. Products updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c