代码之家  ›  专栏  ›  技术社区  ›  user979974

php mysql不使用子字符串执行更新

  •  0
  • user979974  · 技术社区  · 7 年前

    我正面临一个php的问题,它没有在我的脚本中执行一个查询。 sql查询在mysql控制台中运行良好,但没有发生任何事情。年份列保持为空:

    $UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
    mysqli_query($connWarehouse,$UpdateYear) or   die(mysqli_error($connWarehouse)); 
    

    我不知道我做错了什么。这里是完整的脚本:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $db="datawarehouse";
    
    // Create connection
    $connWarehouse = new mysqli($servername, $username, $password, $db);
    
    // Check connection
    if ($connWarehouse->connect_error) {
        die("Connection failed: " . $connWarehouse->connect_error);
    }
    $UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
        mysqli_query($connWarehouse,$UpdateYear) or   die(mysqli_error($connWarehouse));
    
    mysqli_close($connWarehouse));
    ?>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   user979974    7 年前

    我终于解决了。我不知道这样做是否正确。我必须打开到数据库的新连接才能执行。

    更改.php:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $db="datawarehouse";
    
    // Create connection
    $connWarehouse = new mysqli($servername, $username, $password, $db);
    
    // Check connection
    if ($connWarehouse->connect_error) {
        die("Connection failed: " . $connWarehouse->connect_error);
    }
    $AddYear='ALTER TABLE `pat` ADD COLUMN `Year` YEAR;';
    mysqli_query($connWarehouse,$AddYear) or die(mysqli_error($connWarehouse));
    
    mysqli_close($connWarehouse));
    include 'uppat.php';
    ?>
    

    uppat.php文件

    <?php
    require_once 'config-datawarehouse.php';
    
    $UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`DatePrepa`,7,4)';
    
    mysqli_query($conn,$UpdateYear) or die(mysqli_error($conn));
    
    mysqli_close($conn);
    ?>
    

    这样它就执行了更新查询。