Jump to content

[SOLVED] Combining a word with a variable in a mysql query


cliftonbazaar

Recommended Posts

I can't seem to get this line of code working -

$sql = "UPDATE customers SET member'{$_SESSION['referrer']}'='1' WHERE userName='{$_SESSION['userName']}' ";

More specifically it's the

SET member'{$_SESSION['referrer']}'='1'

that I don't know how to do.

 

My database has memberships for different websites; so the fields are 'memberkoala', 'memberalien', 'memberdragon' e.t.c  I always need the work 'member' in there but I also need to add the variable $_SESSION['referrer'] to the filed name.

I'm not sure exactly what you want but i think you are looking for CONCAT :

 

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

 

But if you have many website in the same table why you don't add a column like that

 

website varchar(50)

 

AND

 

UPDATE customers SET referer='$_SESSION['referrer']', website='www.something.com' WHERE username='$_SESSION['userName']';

Remove the single-quotes that are around '{$_SESSION['referrer']}' and if the fields are numeric, remove the single-quotes that are around the '1'

 

You can always echo your $sql variable after it has been assigned the query string to see what exactly is in it.

Remove the single-quotes that are around '{$_SESSION['referrer']}' and if the fields are numeric, remove the single-quotes that are around the '1'

Brilliant, that did the trick  :)

 

You can always echo your $sql variable after it has been assigned the query string to see what exactly is in it.

 

This is one of things that I would never have thought about but once you read it you think "why the heck didn't I think of that before, it's so simple!"

 

Thanks for the replies.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.