Initialising variables
<? php
$host = 'localhost';
$user = 'root';
$pword = '';
$dbname = 'mydb';
$tablename = 'userdata';
$con = mysqli_connect($host, $user, $pword);
Is this the correct code of the table creation function?( Assuming we have connected to the Database)
function createtable($tablename) {
$con = 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)
)";
if (mysqli_query($con,$sql))
{
echo "Table created.";
}
else
{
echo "Error in creating table.";
}
}
?>
Little change. Just suggestion to pass $con
& $tablename
parameters to createtable()
and finally call the createtable()
function.
$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);