jholzy Posted December 5, 2009 Share Posted December 5, 2009 Why doesn't this function work? It works just fine without wrapping it in a function but adding the function to it causes the query not to run. I'm sure there is an easy explanation but I can't figure it out. Maybe the query needs to be moved outside the function... but how? All I know is that this and other similar snippets of code will be reused many times throughout the app and it just makes sense to move it into a function. <?php function display_positions() { // connection for display of player positions. $result = mysql_query("SELECT * FROM positions ORDER BY abbr ASC", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } else { echo "<table class=\"display_table\" summary=\"Displays the defensive player positions.\">" . "\n"; echo "<tr>" . "\n"; echo "<td colspan=\"4\" class=\"display_table_title\">Defensive Player Postions</td>" . "\n"; echo "</tr>" . "\n"; echo "<tr>" . "\n"; echo "<td class=\"display_table_col_head_left\">Long</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Short</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Abbr</td>" . "\n"; echo "</tr>" . "\n"; // continue printing the table with the results of the query. while ($row = mysql_fetch_array($result)) { echo "<tr>" . "\n"; echo "<td class=\"display_table_content_left\">" . $row[2] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[3] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[4] . "\n"; echo "</td>" . "\n"; echo "</tr>" . "\n"; } echo "</table>" . "\n"; } } ?> edit: clarification Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 5, 2009 Share Posted December 5, 2009 This should work, I've done similar things. Try adding: error_reporting(E_ALL); After the <?php tag and tell us if you get any errors. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 5, 2009 Share Posted December 5, 2009 $result = mysql_query("SELECT * FROM positions ORDER BY abbr ASC", $connection); $connection is undefined. Quote Link to comment Share on other sites More sharing options...
jholzy Posted December 5, 2009 Author Share Posted December 5, 2009 This should work, I've done similar things. Try adding: error_reporting(E_ALL); After the <?php tag and tell us if you get any errors. I get nothing when adding this. I actually keep that bit of code active on each page while coding... The page quits loading right after displaying "Database query failed: " without displaying any of the . mysql_error(). It simply quits printing the rest of the page. If I remove the function and call the code directly with an include it works fine. Grrrrr... $result = mysql_query("SELECT * FROM positions ORDER BY abbr ASC", $connection); $connection is undefined. Yep, that was my first thought too but like I said, it works fine without wrapping it with the function. $connection is taken care of before calling this code in a function or include, without the function. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 5, 2009 Share Posted December 5, 2009 Than from what we see, Nothing is wrong. You are not showing us all your code, we cannot tell the problem. Quote Link to comment Share on other sites More sharing options...
jholzy Posted December 5, 2009 Author Share Posted December 5, 2009 $result = mysql_query("SELECT * FROM positions ORDER BY abbr ASC", $connection); $connection is undefined. Looks like I need to dig a little deeper in how to create user defined functions. When you say $connection is undefined do you mean that it needs to be passed to the function? I've not been able to find a good explanation for passing variables to a function. Most of the tutorials I've run across are a little vague in explaining this. Of course, the possibility is pretty good that I'm just having mental malfunction in understanding this one topic. I get the syntax but not the mechanics. Any pointers or tutorial suggestions? @oni-kun: After adding ini_set("display_errors", 1); I was able to retrieve more error information that clarified the problem... Notice: Undefined variable: connection in /path/to/the/file/playerpositions.php on line 5 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /path/to/the/file/playerpositions.php on line 5 Database query failed: Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2009 Share Posted December 5, 2009 I've not been able to find a good explanation for passing variables to a function. How about in the official documentation - http://us2.php.net/manual/en/functions.arguments.php Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 5, 2009 Share Posted December 5, 2009 Why don't you just remove the resource link identifier from the passed perameters and allow mysql to use the last opened connection? $result = mysql_query("SELECT * FROM positions ORDER BY abbr ASC"); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2009 Share Posted December 5, 2009 That WOULD at least eliminate the need to learn how to write and use functions correctly. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 5, 2009 Share Posted December 5, 2009 Meh... If he really cared about that he would be using OOP... If he said he was using multiple database connections I would have to agree with the point you are making, but he didn't. Quote Link to comment Share on other sites More sharing options...
jholzy Posted December 5, 2009 Author Share Posted December 5, 2009 @Andy-H: Uhhh... OOP??? I'm lucky to figure out the difference between an array and a function... I've started a hobby project in an attempt to figure out the basics of a php/mysql app/site. Maybe then OOP? The problem I find is that so many books and tutorials were written FOR beginners but by those who have long forgotten what it was like being a beginner! BTW, removing $connection did the trick. I do remember reading something about PHP being smart enough to figure out the last opened connection on its own, provided it hasn't been closed. Thanks. I'd still like to know how its done by passing the variable to the function though... I'll keep reading. @PFMaBiSmAd: I do spend a great deal of time at php.net and the documentation there, again, written by those who have long forgotten what its really like being a beginner. php.net is really a great resource. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 5, 2009 Share Posted December 5, 2009 oop is an acronym for Object-Oriented Programming, check out the link to see what the manual says about it as I don't regard myself in a position to explain (will probably be slated by someone for being incorrect in some aspect...) You can implement your function to use the resource passed as a parameter like this: function display_positions(&$connection = false) { $query = "SELECT * FROM positions ORDER BY abbr ASC"; /* ** If $connection is set (not false) use as connection resource in query, ** if not, allow mysql to find an open connection. */ if ( isset($connection) ) { $result = mysql_query($query, $connection); } else { $result = mysql_query($query); } if (!$result) { die("Database query failed: " . mysql_error()); } else { echo "<table class=\"display_table\" summary=\"Displays the defensive player positions.\">" . "\n"; echo "<tr>" . "\n"; echo "<td colspan=\"4\" class=\"display_table_title\">Defensive Player Postions</td>" . "\n"; echo "</tr>" . "\n"; echo "<tr>" . "\n"; echo "<td class=\"display_table_col_head_left\">Long</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Short</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Abbr</td>" . "\n"; echo "</tr>" . "\n"; // continue printing the table with the results of the query. while ($row = mysql_fetch_array($result)) { echo "<tr>" . "\n"; echo "<td class=\"display_table_content_left\">" . $row[2] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[3] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[4] . "\n"; echo "</td>" . "\n"; echo "</tr>" . "\n"; } echo "</table>" . "\n"; } } Quote Link to comment Share on other sites More sharing options...
jholzy Posted December 5, 2009 Author Share Posted December 5, 2009 oop is an acronym for Object-Oriented Programming, check out the link to see what the manual says about it as I don't regard myself in a position to explain (will probably be slated by someone for being incorrect in some aspect...) As for OOP, I've read some about it and understand the idea but I still need to get the basic stuff squared in my mind. You can implement your function to use the resource passed as a parameter like this: Ok, I can make out the meaning of most of what you have added and changed. I'll play with this for a while and try to figure out how it works in another query and display. Thanks for taking the time! I'm sure I'll be back here when I hit a wall again. BTW, I've found Lynda.com to be a great site for learning this stuff... still leaves me with a lot of questions though. There is a fee for using it but IMHO its worth the cost. function display_positions(&$connection = false) { $query = "SELECT * FROM positions ORDER BY abbr ASC"; /* ** If $connection is set (not false) use as connection resource in query, ** if not, allow mysql to find an open connection. */ if ( isset($connection) ) { $result = mysql_query($query, $connection); } else { $result = mysql_query($query); } if (!$result) { die("Database query failed: " . mysql_error()); } else { echo "<table class=\"display_table\" summary=\"Displays the defensive player positions.\">" . "\n"; echo "<tr>" . "\n"; echo "<td colspan=\"4\" class=\"display_table_title\">Defensive Player Postions</td>" . "\n"; echo "</tr>" . "\n"; echo "<tr>" . "\n"; echo "<td class=\"display_table_col_head_left\">Long</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Short</td>" . "\n"; echo "<td class=\"display_table_col_head_center\">Abbr</td>" . "\n"; echo "</tr>" . "\n"; // continue printing the table with the results of the query. while ($row = mysql_fetch_array($result)) { echo "<tr>" . "\n"; echo "<td class=\"display_table_content_left\">" . $row[2] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[3] . "\n"; echo "</td>" . "\n"; echo "<td class=\"display_table_content_center\">" . $row[4] . "\n"; echo "</td>" . "\n"; echo "</tr>" . "\n"; } echo "</table>" . "\n"; } } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 5, 2009 Share Posted December 5, 2009 You will want to read up on variable scope. A global variable $connection is different than the variable $connection defined in a function. Php doesn't automatically move to global scope when performing variable name resolution, although some languages do. Quote Link to comment Share on other sites More sharing options...
jholzy Posted December 6, 2009 Author Share Posted December 6, 2009 You will want to read up on variable scope. A global variable $connection is different than the variable $connection defined in a function. Php doesn't automatically move to global scope when performing variable name resolution, although some languages do. That gives me a place to start. Thanks. I took a quick look at the 2 part tutorial in your signature. Looks like some good stuff to review as well. Again, thanks. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 6, 2009 Share Posted December 6, 2009 I wrote those a while ago. Hopefully they are still useful. 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.