Jump to content

Why Is This User Defined Function Broke?


jholzy

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

$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:

 

 

Link to comment
Share on other sites

@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.

 

 

 

 

Link to comment
Share on other sites

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";
}
}

Link to comment
Share on other sites

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";
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.