tcollie Posted June 23, 2007 Share Posted June 23, 2007 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 More sharing options...
teng84 Posted June 23, 2007 Share Posted June 23, 2007 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 https://forums.phpfreaks.com/topic/56798-solved-mysql-arrayforeach-problems/#findComment-280593 Share on other sites More sharing options...
tcollie Posted June 23, 2007 Author Share Posted June 23, 2007 thanks for the help teng. Apparently I've been slamming my head on my desk over nothing. Always have to make a big deal out of everything. Thanks again for your help. Link to comment https://forums.phpfreaks.com/topic/56798-solved-mysql-arrayforeach-problems/#findComment-280601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.