btc_uk Posted January 8, 2007 Share Posted January 8, 2007 Hi,I am trying to load numbers from a list in a MySQL database (I have no problem doing that) then I need to seperate them all out and have a variable for each.For example, I have 6 comma seperated values stored in a datbase (1,2,3,4,5,6 for example) I then need to set a variable for each e.g. $n1 is set to 1, $n2 is set to 2, $n3 is set to 3, and so on (obviously these won't be the actual numbers, otherwise I could have just hard-coded them) and then I will be using these values in the rest of the script.I can not change it so that the values from the database are provided any other way - they are always in this format.Any help you can provide is much appreciated.Ben Quote Link to comment https://forums.phpfreaks.com/topic/33365-help-needed-setting-comma-seperated-values-as-variables/ Share on other sites More sharing options...
HuggieBear Posted January 8, 2007 Share Posted January 8, 2007 OK, so lets say the list in your database looks like this... 1,2,4,8,16,32Would you want it like this:$1 = 1$2 = 2$3 = 4$4 = 8$5 = 16$6 = 32Or this:$1 = 1$2 = 2$4 = 4$8 = 8$16 = 16$32 = 32If I've read it right and it's the latter, then give this a try...[code]<?php// Select csv's from database$sql = "SELECT column_name FROM table_name";$result = mysql_query($sql);$row = mysql_fetch_array($result, MYSQL_ASSOC);// Put each element into an array keyed on number$values = explode(",", $row['column_name']);// Make a variable out of each valueforach ($values as $k => $v){ $$v = $v;}?>[/code]All you need to do is replace my query with yours and change [code=php:0]$row['column_name'][/code] to the name of yor column.RegardsHuggieRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33365-help-needed-setting-comma-seperated-values-as-variables/#findComment-155902 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2007 Share Posted January 8, 2007 PHP doesn't allow variable names that begin with numbers.If you can use arrays instead of individual variables, just use the explode function. If you need individual variables, you can do something like:[code]<?php$sql = "SELECT column_name FROM table_name";$result = mysql_query($sql);$row = mysql_fetch_assoc($result);list($a1,$a2,$a3,$a4,$a5,$a6) = explode(',',$row['column_name']);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/33365-help-needed-setting-comma-seperated-values-as-variables/#findComment-155918 Share on other sites More sharing options...
HuggieBear Posted January 8, 2007 Share Posted January 8, 2007 [quote author=kenrbnsn link=topic=121536.msg499878#msg499878 date=1168277783]PHP doesn't allow variable names that begin with numbers.[/quote]Now I feel stupid, I was in coding mode, making sure everything was syntactically correct, I completely glossed over that fact. :-XRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33365-help-needed-setting-comma-seperated-values-as-variables/#findComment-155977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.