这是php数据库中创建表的正确函数吗?

这是php数据库中创建表的正确函数吗?,php,Php,初始化变量 <? php $host = 'localhost'; $user = 'root'; $pword = ''; $dbname = 'mydb'; $tablename = 'userdata'; $con = mysqli_connect($host, $user, $pword); ?>变化不大。建议将$con&$tablename参数传递到createtable(),最后调用createtable()函数 $host = 'localhost'; $user = '

初始化变量

<? php
$host = 'localhost';
$user = 'root';
$pword = '';
$dbname = 'mydb';
$tablename = 'userdata';
$con = mysqli_connect($host, $user, $pword); 

?>

变化不大。建议将
$con
&
$tablename
参数传递到
createtable()
,最后调用
createtable()
函数

$host = 'localhost';
$user = 'root';
$pword = '';
$dbname = 'mydb';
$tablename = 'userdata';
$con = mysqli_connect($host, $user, $pword, $dbname);
function createtable($con, $tablename) {
//mysqli_connect($host, $user, $pword);
$sql = "CREATE TABLE IF NOT EXISTS
  $tablename(uid int(10) unsigned NOT NULL AUTO_INCREMENT,
  firstname char(60),
  lastname char(60),
  username varchar(60),
  password varchar(60),
  gender enum('male','female') NOT NULL,
  course set('PHP','HTML','CSS','Javascript'),
  comments longtext,
  PRIMARY KEY(uid)
  )";
$result = mysqli_query($con,$sql);
if($result){echo "Table created.";}
else{echo mysqli_error($con);}
}
createtable($con, $tablename);

也许最好是“否”,以及为什么需要一个函数在每个表上创建相同的表call@FerozAkbar因为如果它不存在,我只想创建同一个表一次,但使用我自己的函数,这个问题似乎离题了,因为它询问的是个人辅导和代码检查。
$host = 'localhost';
$user = 'root';
$pword = '';
$dbname = 'mydb';
$tablename = 'userdata';
$con = mysqli_connect($host, $user, $pword, $dbname);
function createtable($con, $tablename) {
//mysqli_connect($host, $user, $pword);
$sql = "CREATE TABLE IF NOT EXISTS
  $tablename(uid int(10) unsigned NOT NULL AUTO_INCREMENT,
  firstname char(60),
  lastname char(60),
  username varchar(60),
  password varchar(60),
  gender enum('male','female') NOT NULL,
  course set('PHP','HTML','CSS','Javascript'),
  comments longtext,
  PRIMARY KEY(uid)
  )";
$result = mysqli_query($con,$sql);
if($result){echo "Table created.";}
else{echo mysqli_error($con);}
}
createtable($con, $tablename);