Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sir,
please tell how to create table using below code

What I have tried:

$table_name = "test";
$a ="r1,r2,r3,r4";
$abc = (explode(",",$a));
foreach($abc as $x)
{

echo $x;
$query = "CREATE TABLE $table_name".
'(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
'.$x.' varchar(30))'

if($con->exec($query))
{
echo "Table MyGuests created successfully";
}
else
{
echo "try again";
}

}

after this the table should created in database but it is not.Why I am getting the solution.Please guide me
Posted
Updated 1-Dec-16 0:57am
v6
Comments
Peter Leow 1-Dec-16 6:41am    
I can see that you have amended your code based on my Solution 1. But then, look carefully at your amended code, you are very careless by introducing new mistakes. I'm sure you have encountered error messages, look at the error messages and debug it yourself. Do not too quick to jump to ask question. You can't move too far this way.

Look into these three points:
1. What is
$a = r1,r2,r3,r4;

? Do you mean
$a = "r1,r2,r3,r4";

?
2. The $xyz is assigned the <p> tag:

$xyz = "<p>".$abc."</p>";

which got included into the field name.
SQL
"CREATE TABLE $table_name".
    			'(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
			  	  '.$xyz.' varchar(30))'

I do not think that is what you want.
3. What is the purpose of $x? It did not seem to do anything useful.
 
Share this answer
 
v5
$table_name = "test";
$a ="r1,r2,r3,r4";

i want to explode this
$products = (explode(",",$a));

after exploding i will get the answer as

foreach($a as $x)
{
echo $x;
}

$x through these variable i want to create column in table in database.

i wrote the query as follow
SQL
"CREATE TABLE $table_name".
    '(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    '.$x.' varchar(30))'


after this the table should be created in database but it is not.Why I am getting the solution.Please guide me
 
Share this answer
 
v2
Comments
Peter Leow 1-Dec-16 6:42am    
This is not the solution. Use the improve question button instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900