EnglishAdam
EnglishAdam

Reputation: 1390

PHP / MySql - Concatenation in query

I am having problems concatenating a variable to a number (with the variable first, because $info1002 is not a known variable), I appreciate my problem here must be my single double quotations and I've tried a lot of combinations and my googling hasn't helped.

mysql_query("INSERT INTO users (ID, info1) VALUES ('','.$info.''002')")or die(mysql_error());

Upvotes: 1

Views: 1209

Answers (4)

Svetoslav
Svetoslav

Reputation: 4676

mysql_query("INSERT INTO users (info1) VALUES ('{$info}002')")or die(mysql_error());

It may not work only if your ID is set NOT NULL and it not set as autoincrement!

Upvotes: 0

cassi.lup
cassi.lup

Reputation: 1261

you need to format it like this:

mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002')") or die(mysql_error());

Also, if you have the ID field set to AutoIncrement, you can ommit it like this:

mysql_query("INSERT INTO users (info1) VALUES ('".$info."002')") or die(mysql_error());

This will insert value of $info followed by 002 into the database.

Upvotes: 4

user745235
user745235

Reputation:

why don't you concatenate if before adding it to the query? I think it is much easier so you don't have that mass with quotation.

$var = $info.'002';
mysql_query("INSERT INTO users (ID, info1) VALUES (null ,'".$var."') or die(mysql_error());

Upvotes: 1

Rab
Rab

Reputation: 35572

mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002"')")or die(mysql_error());

Upvotes: 2

Related Questions