Jump to content

[SOLVED] How to take an array...


The14thGOD

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/159945-solved-how-to-take-an-array/
Share on other sites

<?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 />";
}



?>

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;
				}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.