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



?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.