neridaj Posted February 28, 2008 Share Posted February 28, 2008 Hello, I have a function that is supposed to return the columns and rows from a db table and pass it on to another function for output. I know this is a routine procedure, but I'm still green with multidimensional arrays and haven't quite got my head around it. Here is the code I have: function get_users() { //extract from the database all the user data $conn = db_connect(); $result = $conn->query( "select username, firstname, lastname, email from user"); if (!$result) return false; //create an array of the user data $user_array = array(); for ($count = 1; $row = $result->mysql_fetch_row(); ++$count) { $user_array[$count] = $row[0]; } return $user_array; } function display_users($user_array) { // set global variable, so we can test later if this is on the page global $user_table; $user_table = true; ?> <br /> <form name='user_table' action='delete_users.php' method='post'> <table width=300 cellpadding=2 cellspacing=0> <?php $color = "#000000"; echo "<tr bgcolor='$color'><td><strong>User</strong></td>"; echo "<td><strong>Delete?</strong></td></tr>"; if (is_array($user_array) && count($user_array)>0) { while ($row = mysql_fetch_array($user_array, MYSQL_ASSOC)) { if ($color == "#cccccc") $color = "#ffffff"; else $color = "#cccccc"; printf ("<tr bgcolor='$color'><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $row["username"], $row["firstname"], $row["lastname"], $row["email"]); echo "</tr>"; } mysql_free_result($user_array); } Thanks for any help, Jason Quote Link to comment Share on other sites More sharing options...
rbrown Posted February 28, 2008 Share Posted February 28, 2008 I'm not a wizard on arrays but I have found you have to create the array outside the function then call it in the function with "&$my_array" Then it will return your array. So you would have something like this: //create an array of the user data $user_array = array(); function get_users($conn,&$user_array) { //extract from the database all the user data $conn = db_connect(); $result = $conn->query( "select username, firstname, lastname, email from user"); if (!$result) return false; for ($count = 1; $row = $result->mysql_fetch_row(); ++$count) { $user_array[$count] = $row[0]; } } Hope this helps point you in the right direction... Quote Link to comment Share on other sites More sharing options...
neridaj Posted February 28, 2008 Author Share Posted February 28, 2008 I figured out why no data was being returned, now I get this error: "supplied argument is not a valid MySQL result" which I'm assuming is because I am returning a multidimensional array to mysql_fetch_array() incorrectly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.