user3815049
user3815049

Reputation:

I want to alter table in php , but my code is not working

I want add another column into my table using PHP my code is not working

$sql="ALTER TABLE user_preference_table ADD column '$tag_id' VARCHAR(60) ";
$result = $conn->query($sql);

i think problem is with my way of declaring the variable into the query ?

is my query right?

'$tag_id' 

it is a variable that contains the some id like 501

Upvotes: 1

Views: 1582

Answers (4)

Partha Mitra
Partha Mitra

Reputation: 130

You are trying to create the column with numeric value '501'. I think you should add some character before $tag_id.

Try this:

$tag_id.='F'.$tag_id;

$sql="ALTER TABLE user_preference_table ADD column '$tag_id' VARCHAR(60) ";

Upvotes: 0

Faruk Omar
Faruk Omar

Reputation: 1193

You no need to use column and '' Please try this

$sql="ALTER TABLE user_preference_table ADD  $tag_id VARCHAR(60) ";
$result = $conn->query($sql);

Upvotes: 0

Pupil
Pupil

Reputation: 23948

Replace single quote with backtick.

$sql="ALTER TABLE user_preference_table ADD column `$tag_id` VARCHAR(60) ";

Signle Quotes are generally for inserting values into Database tables.

Backticks are used for DB fields.

They prevent errors of using reserved keywords in MySQL.

e.g.

as
from

...etc

Upvotes: 2

Lalit Sharma
Lalit Sharma

Reputation: 565

try this

$sql="ALTER TABLE user_preference_table ADD column `".$tag_id."` VARCHAR(60) ";

Upvotes: 1

Related Questions