Jump to content

[SOLVED] MySQL Array/foreach problems


tcollie

Recommended Posts

Basically what I want to do is this.  I have table with about 20 fields in it.  I want to be able to dynamically pull the data for a certain record and create an array with the keys as the fields names and of course the data as the value.

 

Here's what I have so far (and it's not working  >:(  ):

 

function get_data($record_id)
  {
  $query = mysql_query("SELECT * FROM table where record_id = '$record_id'") 
  or die(mysql_error());  
  while($row = mysql_fetch_array($query )) {
  foreach ($row as $key => $value) { 
  $data .= "&$key=$value";
  }
  }  
  return $data;
  }

 

Now I want to be able to call the function and work with the data I'm trying to call.

 

$record_id = 'xxxxxxxxx';
$test = get_data($record_id);
$name = $test['username']; //This would be the username field from the table
echo $name;

 

All I get is a blank screen when I run this and I know the problem is in the foreach statement I tried to write.  Any suggestions here?

 

Thanks in advance.

Link to comment
Share on other sites

 

function get_data($record_id)

  {

  $query = mysql_query("SELECT * FROM table where record_id = '$record_id'")

  or die(mysql_error()); 

$row = mysql_fetch_assoc($query );

  }

  } 

  return $row ;

  }

 

then you get an array result

 

:-*

 

 

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.