i have a table tracker_item that looks like this
tracker_item
id heading trackerid
1 name 1
2 location 1
3 age 1
4 candidate 2
5 area 2
I wish to create different database table according to trackerid using the parameters that are below heading. E.g acc to the above table tracker_item i want that 2 tables should get created
table1
id name location age
table 2
id candidate area
The code that i tried is
$sql="SELECT * FROM `tracker_item` where trackerid='".$trackerid."' ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo $sql1 = "CREATE TABLE item (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
".
$row['heading']
." VARCHAR(30) NOT NULL,
resume VARCHAR(50)
)";
}
if (mysqli_query($con, $sql1))
{
echo "Table created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
}
output for $sql1 that i got was
CREATE TABLE item (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, resume VARCHAR(50) )
CREATE TABLE item (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, location VARCHAR(30) NOT NULL, resume VARCHAR(50) )
CREATE TABLE item (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, age VARCHAR(30) NOT NULL, resume VARCHAR(50) )
Instead of multiple tables i would like to get o/p of $sql1 look like the following so that a table can be created in database
CREATE TABLE item ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, location VARCHAR(30) NOT NULL, age VARCHAR(50) )
Can anyone please tell how it can be done