gpt4 book ai didi

php - 如何在php mysql中创建表和字段

转载 作者:行者123 更新时间:2023-11-30 00:05:03 24 4
gpt4 key购买 nike

我尝试通过 php/mysql 创建一个表及其文件

所以,首先我创建了一个 sql 类:

<?php
class db{


public $db;
function __construct(){
$this->db_connect('localhost','root','','dbmenu');
}

function db_connect($host,$user,$pass,$database){
$this->db = new mysqli($host, $user, $pass, $database);

if($this->db->connect_errno > 0){
die('Unable to connect to database [' . $this->db->connect_error . ']');
}
}

function setTables()
{

//for created Groups
$_tableName="groups";
$_fieldsName=array();
$_fieldsName = array("id" =>"id int(10) NOT NULL AUTO_INCREMENT",
"name" =>"name varchar(25) COLLATE utf8_persian_ci NOT NULL",
"parent" =>"element varchar(20) COLLATE utf8_persian_ci NOT NULL",
"primary" =>"PRIMARY KEY (`id`)");
$this->createTable($_tableName,$_fieldsName);
}

function createTable($_tableName,$_fieldsName)
{

}
} ?>

有人可以指导我编写createTable($_tableName,$_fieldsName)函数吗?

$db= new db;
$db=setTables();

最佳答案

根据你之前的代码,它会是这样的

function createTable($_tableName,$_fieldsName)
{
$this->db->query('CREATE TABLE '. $_tableName .
' (' . join(',', $_fieldsName) .');');
}

关于php - 如何在php mysql中创建表和字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24638015/

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