xjermx Posted July 12, 2010 Share Posted July 12, 2010 I've tried re-reading and re-re-reading the available info on arrays, but I'm just not understanding what I'm doing wrong, or how I can effectively troubleshoot it. My question is this: How do I do a mysql query inside a function, and then let the main page, or another function on the main page use the array that the query produces? Here is some code: <?php myfunction1(); $rowCheck = mysql_num_rows($gNames); print $rowCheck; while ($row = mysql_fetch_assoc($gNames)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; print_r($row); foreach ($row as $col=>$val){ // if ($col == 'name') { print " $val, \n"; // } } // end foreach foreach ($row as$val){ print " $val, \n"; } // end foreach }// end while function myfunction1() { global $gNames; $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database."); mysql_select_db("databasename", $db) or die ("Couldn't select the database."); $result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db); // modified to only show for user 1 $gNames = $result; $rowCheck = mysql_num_rows($result); if($rowCheck > 0){ echo 'You do have characters, they should be listed here..<br>'; while ($row = mysql_fetch_assoc($result)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; //debugging print_r($row); //debugging echo "<br>"; foreach ($row as $col=>$val){ if ($col == 'name') { print " $val, \n"; } } // end foreach }// end while print "<br><br>"; } else { echo 'You have no characters. You should create one.'; } } /////////////////////////////// end function ?> Basically, the query goes just fine inside the function, it displays its data, it works like a dream. But I cannot seem to be able to pass it off to the main part of the page. the rowCheck in the main part returns "3", indicating that it has data in it, but I can't touch/view it. Is there a trick that I don't know, or something basic that I'm overlooking/not understanding? Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/ Share on other sites More sharing options...
BillyBoB Posted July 12, 2010 Share Posted July 12, 2010 when reading this the only thing i though of was returning the value from the function. <?php $returnVal = myfunction1(); //collect data here $rowCheck = mysql_num_rows($returnVal); print $rowCheck; while ($row = mysql_fetch_assoc($returnVal)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; print_r($row); foreach ($row as $col=>$val){ // if ($col == 'name') { print " $val, \n"; // } } // end foreach foreach ($row as$val){ print " $val, \n"; } // end foreach }// end while function myfunction1() { global $gNames; $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database."); mysql_select_db("databasename", $db) or die ("Couldn't select the database."); $result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db); // modified to only show for user 1 $gNames = $result; $rowCheck = mysql_num_rows($result); if($rowCheck > 0){ echo 'You do have characters, they should be listed here..<br>'; while ($row = mysql_fetch_assoc($result)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; //debugging print_r($row); //debugging echo "<br>"; foreach ($row as $col=>$val){ if ($col == 'name') { print " $val, \n"; } } // end foreach }// end while print "<br><br>"; } else { echo 'You have no characters. You should create one.'; } return $gNames; // return value here } /////////////////////////////// end function ?> Good luck and have fun Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/#findComment-1085043 Share on other sites More sharing options...
msaz87 Posted July 12, 2010 Share Posted July 12, 2010 When I read this, it looked like you were having an ordering issue... this part of the code: myfunction1(); $rowCheck = mysql_num_rows($gNames); print $rowCheck; while ($row = mysql_fetch_assoc($gNames)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; print_r($row); foreach ($row as $col=>$val){ // if ($col == 'name') { print " $val, \n"; // } } // end foreach foreach ($row as$val){ print " $val, \n"; } // end foreach }// end while Has to go after this part: function myfunction1() { global $gNames; $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database."); mysql_select_db("databasename", $db) or die ("Couldn't select the database."); $result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db); // modified to only show for user 1 $gNames = $result; $rowCheck = mysql_num_rows($result); if($rowCheck > 0){ echo 'You do have characters, they should be listed here..<br>'; while ($row = mysql_fetch_assoc($result)){ echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; //debugging print_r($row); //debugging echo "<br>"; foreach ($row as $col=>$val){ if ($col == 'name') { print " $val, \n"; } } // end foreach }// end while print "<br><br>"; } else { echo 'You have no characters. You should create one.'; } } /////////////////////////////// end function Since you haven't defined the function yet. As far as passing the array into a new function, you have to include it in the function($var) and make sure anything you want out of the function is set as a global variable. Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/#findComment-1085044 Share on other sites More sharing options...
Pikachu2000 Posted July 12, 2010 Share Posted July 12, 2010 How are you calling the function? Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/#findComment-1085046 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2010 Share Posted July 12, 2010 use the array that the query produces? By returning the array from the function - i.e return $row; Functions are designed to accept (optional) parameters in the function call, perform some operation and in most cases return the results so that you can either assign the results to a program variable or use the returned results as a parameter in another function call - $program_variable = your_function('parameter1','parameter2'); or $program_variable = your_function1(your_function2('parameter1','parameter2')); Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/#findComment-1085049 Share on other sites More sharing options...
xjermx Posted July 12, 2010 Author Share Posted July 12, 2010 Thanks for the responses. I tried what BillyBob and PFMaBiSmAd suggested. I stuck return $gNames; // return value here at the tail end of my function Here is part is my code again, with a few more printed debug lines: <?php print "<h5>Debug line 1. Top of program.</h5>"; $returnVal = myfunction1(); //collect data here $rowCheck = mysql_num_rows($returnVal); print $rowCheck; while ($row = mysql_fetch_assoc($returnVal)){ print "<h5>Debug line 2. Start of WHILE</h5>"; echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; print_r($row); foreach ($row as $col=>$val){ // if ($col == 'name') { print " $val, \n"; // } } // end foreach foreach ($row as$val){ print " $val, \n"; } // end foreach }// end while function myfunction1() { global $gNames; $db = mysql_connect("blahblahblah", "myuser", "mypass") or die ("Error connecting to database."); mysql_select_db("databasename", $db) or die ("Couldn't select the database."); $result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db); // modified to only show for user 1 $gNames = $result; $rowCheck = mysql_num_rows($result); print "<h5>Debug line 3. inside MYFUNCTION1</h5>"; if($rowCheck > 0){ echo 'You do have characters, they should be listed here..<br>'; while ($row = mysql_fetch_assoc($result)){ print "<h5>Debug line 4. inside MYFUNCTION1, inside WHILE</h5>"; echo is_array($row) ? 'Array<br>' : 'not an Array<br>'; //debugging print_r($row); //debugging echo "<br>"; foreach ($row as $col=>$val){ if ($col == 'name') { print " $val, \n"; } } // end foreach }// end while print "<br><br>"; } else { echo 'You have no characters. You should create one.'; } return $gNames; // return value here } /////////////////////////////// end function ?> Note that I'm not seeing "Debug Line 2", which means that it is not getting into the while statement there at the top. Quote Link to comment https://forums.phpfreaks.com/topic/207534-trouble-with-mysql-data-in-arrays/#findComment-1085059 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.