miracle_potential Posted May 15, 2008 Share Posted May 15, 2008 Hey guys me (again) lol Out of interest is it possible to explode a mysql_fetch_array. What I've done is used 4 mysql queries to select 4 random rows and just echo'd them but I really want to cut the code up and make it something I can explode and echo as a set variable. can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/ Share on other sites More sharing options...
kenrbnsn Posted May 15, 2008 Share Posted May 15, 2008 Please post the code you want to clean up and a better explanation of what you're trying to do. Ken Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542048 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 you can explode each value in the array, but not the array its self. foreach($array as $key => $val){ $newArr = explode(" ",$val); // split by spaces // Do some stuff with this new array } Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542053 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 do you mean <?php $username = $row['username']; $password = $row['password']; $etc = $row['etc']; //or extract($row); //or foreach($row as $k => $v) { $$k = $v; } //or list($username, $password, $ect) = $row; ?> all do the same Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542057 Share on other sites More sharing options...
Barand Posted May 15, 2008 Share Posted May 15, 2008 do you mean <?php $username = $row['username']; $password = $row['password']; $etc = $row['etc']; //or extract($row); //or foreach($row as $k => $v) { $$k = $v; } //or list($username, $password, $ect) = $row; ?> all do the same Not quite. extract() requires an associative array (eg from mysql_fetch_assoc() ) list() requires an indexed array (eg from mysql_fetch_row() ) Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542190 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 true, but mysql_fetch_array can fetch both associative and indexed (by default) using $row = mysql_fetch_array($result, MYSQL_BOTH); EDIT: No no Barand your very correct , (now i think about it) i get your point Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542244 Share on other sites More sharing options...
Barand Posted May 15, 2008 Share Posted May 15, 2008 In that case foreach($row as $k => $v) { $$k = $v; } gives every value twice Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542303 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 yeah, i know (after you posted, Not quite. post) but you can tweak the fetch to fit the build.. if you get what i mean Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542311 Share on other sites More sharing options...
miracle_potential Posted May 16, 2008 Author Share Posted May 16, 2008 Lol, basically I need to query the database and get 4 random rows, which I can then on an external page echo as 4 differant products. Laid out like this, $var_1_pic$var_2_pic$var_3_pic$var_4_pic $var_1$var_2$var_3$var_4 pic obviously meaning a picture name I have in the database I'm not really that good with SQL implementation thus I'm stuck I dont really want to have to write out a differant query 4 times when I know I can explode the result of a query and assign it to differant variables. Quote Link to comment https://forums.phpfreaks.com/topic/105779-explode-and-sql/#findComment-542823 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.