TEENFRONT Posted December 29, 2008 Share Posted December 29, 2008 Hey When building a new section on my site, i find it very laborious (sp?) going through and setting all the row names to useable vars, checking which row names i have, and if iv created them all in the results etc. so on the results i do this.. $id = $row['id']; $tname = $row['tname']; $ttag = $row['ttag']; $members = $row['members']; $rating = $row['rating']; $stars = $row['ratingstar']; $rrating = $row['rrating']; is there not a simpler way? Can i not just tell php to do that for me, make vars of all the row names that come back from the query. This would be Soooooo much easier. I know $row['rrating'] is usuable and i dont NEED to set it to a prettier $rrating = $row['rrating']; - but i find $rrating easier to work with than $row['rrating']. So yes, is there a function that returns all the rows in the query, and sets a nice var name from the row name. so something like this.. $sql = mysql_query('select * from this and that etc'); magic_function_here($sql); echo $username; echo $rrating; Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/ Share on other sites More sharing options...
DarkWater Posted December 29, 2008 Share Posted December 29, 2008 Use extract() on the array returned from mysql_fetch_assoc(). Be careful, as it WILL overwrite variables that already exist with the same name. Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725177 Share on other sites More sharing options...
opalelement Posted December 29, 2008 Share Posted December 29, 2008 if $row is the mysql_fetch_array, try this: foreach(mysql_fetch_array($result, MYSQL_ASSOC) as $row => $value) { ${$row} = $value; } completely untested and probably wrong because I don't use foreach much and I actually just learned that ${$var} thing like half an hour ago Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725178 Share on other sites More sharing options...
TEENFRONT Posted December 29, 2008 Author Share Posted December 29, 2008 OMG!!! That is Genius!! I used the Extract with mysql_fetch_assoc, bang it does it all for you. You have no idea how much easier it makes it lol. When buidling something new the worst part for me was going through all the results and setting/checking var names etc etc. Nice nice nice nice nice!! Quick question, i normally use mysql_fetch_array... whats the difference with _assoc? Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725180 Share on other sites More sharing options...
DarkWater Posted December 29, 2008 Share Posted December 29, 2008 OMG!!! That is Genius!! I used the Extract with mysql_fetch_assoc, bang it does it all for you. You have no idea how much easier it makes it lol. When buidling something new the worst part for me was going through all the results and setting/checking var names etc etc. Nice nice nice nice nice!! Quick question, i normally use mysql_fetch_array... whats the difference with _assoc? mysql_fetch_array() stores the array with both numeric indexes and string indexes. _assoc only performs the latter, which is what you want for extract(). Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725181 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2008 Share Posted December 29, 2008 Why do you want to store everything in new variables when you can just use the $row['index'] variable? Ken Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725184 Share on other sites More sharing options...
TEENFRONT Posted December 29, 2008 Author Share Posted December 29, 2008 erm... I know $row['rrating'] is usuable and i dont NEED to set it to a prettier $rrating = $row['rrating']; - but i find $rrating easier to work with than $row['rrating']. Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725189 Share on other sites More sharing options...
opalelement Posted December 29, 2008 Share Posted December 29, 2008 i like having it that way, if im making a user system and want to have its id, but have ids for other stuff like posts, it will get mixed... now if i want something for the current user, i just use $user['id'] and then i know what id it is mysql_fetch_array() stores the array with both numeric indexes and string indexes. _assoc only performs the latter, which is what you want for extract(). mine specifies MYSQL_ASSOC but if you don't wnat that in tehre you can jsut use _assoc Quote Link to comment https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725568 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.