gpt4 book ai didi

php函数不向数据库表添加列

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:40 26 4
gpt4 key购买 nike

我在使用一个非常通用的 php 函数时遇到了问题。

其背后的想法是,如果不存在,则向表中添加一个(TEXT 类型)列。我的php如下:

function add_column($conn, $table, $colname){

// Prepare and execute statment 1
$sql = $conn->prepare("SHOW COLUMNS FROM $table LIKE '$colname'");
if (!$sql->execute()){

// Prepare and execute statment 2
$sql1 = $conn->prepare("ALTER TABLE `ExamServer`.$table ADD `$colname` TEXT");
$sql1->execute();
}
}

感谢任何帮助,谢谢!!

最佳答案

You can check if column exists using following query:

SHOW COLUMNS FROM  `$table` WHERE field = '$colname'

如果有列,您将得到一行。如果不是,您可以执行 ALTER 查询。

if($sql->num_rows == 0) {
// Perform your alter query here
}

关于php函数不向数据库表添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com