jandrews3 Posted March 7, 2008 Share Posted March 7, 2008 I have a list of variables with identical names except for the final character, BUT they are NOT in an array. I understand arrays, but I'm passing these variables from a form in a previous page. I'm trying to use the variable $count to change from $name1 to $name2 to $name3, etc... and $uname1 to $uname2 to $uname3, etc... for each loop. I have the most terrible feeling that this is an elementary problem with an elementary solution. Thanks for any help. $link = mysql_connect("••••••","••••••","••••••") or die("Could not connect: ".mysql_error()); mysql_select_db("ositoweb") or die("Could not select database: ".mysql_error()); $query = "SELECT id, title, continent, orden, fname, lname, uname FROM vp_data ORDER BY continent, orden"; $result = mysql_query($query) or die("Could not perform query: ".mysql_error()); $count = 1; while ($row = mysql_fetch_array($result)) { $query1 = "Update vp_data SET uname = '$uname".$count."' WHERE title='$name".$count."'"; $result1 = mysql_query($query1); print $query1."<br>"; $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/ Share on other sites More sharing options...
revraz Posted March 7, 2008 Share Posted March 7, 2008 Try removing the double quotes around $count Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486141 Share on other sites More sharing options...
puritania Posted March 7, 2008 Share Posted March 7, 2008 you should really use arrays. But have a look at variable variables $variableName = 'test'; echo ${$variableName}; // ouputs the content of $test Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486142 Share on other sites More sharing options...
jandrews3 Posted March 7, 2008 Author Share Posted March 7, 2008 Removing the double quotes didn't work, and I'm not sure how to implement the variablenames suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486149 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 Redo your form so that it uses an array. Variable variables are 3 times slower than using an array. Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486151 Share on other sites More sharing options...
roopurt18 Posted March 7, 2008 Share Posted March 7, 2008 Change: $query1 = "Update vp_data SET uname = '$uname".$count."' WHERE title='$name".$count."'"; to $tmp_uname = {'uname' . $count}; $tmp_title = {'name' . $count}; $query1 = "UPDATE `vp_data` SET `uname` = '{$tmp_uname}' WHERE title={$tmp_title}"; Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486155 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 You need one more $ on each of those references to use the contents as a variable name instead of using the contents as is. Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486170 Share on other sites More sharing options...
roopurt18 Posted March 7, 2008 Share Posted March 7, 2008 Oops. $tmp_uname = ${'uname' . $count}; $tmp_title = ${'name' . $count}; Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486187 Share on other sites More sharing options...
jandrews3 Posted March 7, 2008 Author Share Posted March 7, 2008 GREAT! That seemed to work. $link = mysql_connect("•••••","•••••","•••••") or die("Could not connect: ".mysql_error()); mysql_select_db("ositoweb") or die("Could not select database: ".mysql_error()); $query = "SELECT id, title, continent, orden, fname, lname, uname FROM vp_data ORDER BY continent, orden"; $result = mysql_query($query) or die("Could not perform query: ".mysql_error()); $count = 1; while ($row = mysql_fetch_array($result)) { $tmp_uname = ${'uname' . $count}; $tmp_title = ${'name' . $count}; $query1 = "UPDATE vp_data SET uname = '{$tmp_uname}' WHERE title={$tmp_title}"; $result1 = mysql_query($query1); print $query1."<br>"; $count++; } But the data isn't being stored in the database. The print $query1 line is properly displaying the data as it should: UPDATE vp_data SET uname = 'jaandrws' WHERE title=VP Africa UPDATE vp_data SET uname = 'jandrews' WHERE title=VP Central and Western Africa UPDATE vp_data SET uname = 'aaslestad' WHERE title=VP India UPDATE vp_data SET uname = 'George' WHERE title=VP Bangladesh and Pakistan UPDATE vp_data SET uname = '2828' WHERE title=VP Japan UPDATE vp_data SET uname = '2014' WHERE title=VP Asia Minor and Northern Africa UPDATE vp_data SET uname = '3324' WHERE title=VP Southeast Asia UPDATE vp_data SET uname = 'mzober' WHERE title=VP Pacific Coastal UPDATE vp_data SET uname = '3701' WHERE title=VP Northeast Australia UPDATE vp_data SET uname = 'rjhaddad' WHERE title=VP Southwest Australia UPDATE vp_data SET uname = 'giianor' WHERE title=VP New Zealand UPDATE vp_data SET uname = '1473' WHERE title=VP Great Britain and Ireland UPDATE vp_data SET uname = '2141' WHERE title=VP Northern Europe UPDATE vp_data SET uname = 'Labgal47' WHERE title=VP Scandinavia UPDATE vp_data SET uname = 'adel' WHERE title=VP Germany UPDATE vp_data SET uname = '2211' WHERE title=VP Southern Europe UPDATE vp_data SET uname = 'kevinof' WHERE title=VP France UPDATE vp_data SET uname = '3703' WHERE title=VP Central America and Mexico UPDATE vp_data SET uname = '1587' WHERE title=VP Caribbean and Atlantic Islands UPDATE vp_data SET uname = '841' WHERE title=VP South America UPDATE vp_data SET uname = '3541' WHERE title=VP California USA UPDATE vp_data SET uname = 'atalbot' WHERE title=VP Northwest USA UPDATE vp_data SET uname = '3398' WHERE title=VP Plains USA UPDATE vp_data SET uname = '3613' WHERE title=VP Southwest USA UPDATE vp_data SET uname = '3493' WHERE title=VP Northeast USA UPDATE vp_data SET uname = '3398' WHERE title=VP Midwest USA UPDATE vp_data SET uname = 'zaleski' WHERE title=VP Southeast USA UPDATE vp_data SET uname = '1783' WHERE title=VP Midsouth USA UPDATE vp_data SET uname = 'George' WHERE title=VP Alaska USA UPDATE vp_data SET uname = '2828' WHERE title=VP Canada UPDATE vp_data SET uname = '2014' WHERE title=VP Eastern Canada but it isn't storing the data. The database name is acurrate as are the titles. I'm puzzled. Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486205 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 That value for the title needs single-quotes around it to make it a string and your query is failing as a result, but there is no error checking in the code to tell you that it is failing. Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486212 Share on other sites More sharing options...
jandrews3 Posted March 7, 2008 Author Share Posted March 7, 2008 THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/94904-incrementing-variable-inside-quote/#findComment-486220 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.