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
https://forums.phpfreaks.com/topic/56798-solved-mysql-arrayforeach-problems/
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

 

:-*

 

 

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.