The14thGOD Posted May 27, 2009 Share Posted May 27, 2009 I have no idea how to look this up so any tips are much appreciated. I want to take an array from a database ($row in this case) and keep the associative name as a variable. I'm making 1 page that inserts/updates/deletes a blog entries. So instead of adding a crap ton of if's/echo's for new variables (eg $row['body']) I figured it would be better (less lines of code) to make $row['body'] into $body. Any ideas? I've never been great with arrays so sorry if this is a really easy question. My first idea was to use explode('[') and then I remember vaguely (trying to track it down right now) a method to make text into a new variable using ${} or something. Thanks again, Justin Quote Link to comment https://forums.phpfreaks.com/topic/159945-solved-how-to-take-an-array/ Share on other sites More sharing options...
BobcatM Posted May 27, 2009 Share Posted May 27, 2009 <?php $query = "SELECT * FROM example"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['name']. " - ". $row['age']; // Or you can loop it. while($row = mysql_fetch_array($result)){ echo $row['name']. " - ". $row['age']; echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159945-solved-how-to-take-an-array/#findComment-843578 Share on other sites More sharing options...
The14thGOD Posted May 27, 2009 Author Share Posted May 27, 2009 thanks for responding BobcatM. However, that is not what I want =O I managed to get it though after throwing 2 snippets together it was just sitting there. Here it is for anyone who might have the problem later on: <?php $query = "SELECT * FROM news WHERE id=$_GET[id] LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); foreach($row as $var => $value) { ${$var} = $value; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159945-solved-how-to-take-an-array/#findComment-843581 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.