Jump to content

variable scope...again...


mxl

Recommended Posts

Last time, I thought I had this problem licked, but it seems to have brought it's head out again!

 

Here's the code that works:

 

<?php

$pid = '695370';

 

$conn = mysql_connect ("localhost", "pwd", "pwd") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("prefseat_events", $conn);

 

 

$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";

mysql_query("SET concerts, 'utf8'" );

 

$result = mysql_query($q_string);

if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)

{

    echo 'There are no results!';

    mysql_close($conn);

    exit;

}

else

{  //$result === TRUE

  // fetch the data.  It should only come in as one row because $pid is unique.

  while($row_data = mysql_fetch_array($result))

  {

    $event=$row_data['event'];

      $venueName=$row_data['venueName'];

      $venueAddress=$row_data['venueAddress'];

      $venueCity=$row_data['venueCity'];

      $venueState=$row_data['venueState'];

      $venueZIP=$row_data['venueZIP'];

      $venueSeatingChart=$row_data['venueSeatingChart'];

      $weekday=$row_data['weekday'];

      $month=$row_data['month'];

      $day=$row_data['day'];

      $year=$row_data['year'];

      $time=$row_data['time'];

      $pid2=$row_data['pid2'];

      $time2=$row_data['time2'];

      $data = <<<EOM

EOM;

echo $data;

  }

} //$result === TRUE

 

$title = $event.' Tickets '.$venueCity.', '.$venueState.' at '.$venueName.' '.$month.' '.$day.', '.$year.' - Buy '.$event.' Tickets for '.$venueCity.', '.$venueState.' '.$venueName;

 

... ?>

 

This works, no problem.  I'd like to reuse some of the code and put it into a simple require_once file and pass what I need to the only function in the included file called concert_db.php

 

 

 

 

Here's my changed code:

 

My original page now looks like this:

 

<?php

$pid = '695370';

 

require_once('../../php/concert_db.php');

 

concert_connect($pid);

 

$title = $event.' Tickets '.$venueCity.', '.$venueState.' at '.$venueName.' '.$month.' '.$day.', '.$year.' - Buy '.$event.' Tickets for '.$venueCity.', '.$venueState.' '.$venueName;

 

 

... ?>

 

 

Here's the content of concert_db.php:

 

<?

 

function concert_connect($pid)

{

$conn = mysql_connect ("localhost", "prefseat_reader", "prefseat_reader") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("prefseat_events", $conn);

 

echo "connected";

 

$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";

mysql_query("SET concerts, 'utf8'" );

 

$result = mysql_query($q_string);

if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)

{

    echo 'There are no results!';

    mysql_close($conn);

    exit;

}

else

{  //$result === TRUE

  // fetch the data.  It should only come in as one row because $pid is unique.

  while($row_data = mysql_fetch_array($result))

  {

    $event=$row_data['event'];

      $venueName=$row_data['venueName'];

      $venueAddress=$row_data['venueAddress'];

      $venueCity=$row_data['venueCity'];

      $venueState=$row_data['venueState'];

      $venueZIP=$row_data['venueZIP'];

      $venueSeatingChart=$row_data['venueSeatingChart'];

      $weekday=$row_data['weekday'];

      $month=$row_data['month'];

      $day=$row_data['day'];

      $year=$row_data['year'];

      $time=$row_data['time'];

      $pid2=$row_data['pid2'];

      $time2=$row_data['time2'];

      $data = <<<EOM

EOM;

echo $data;

  }

} //$result === TRUE

}

 

echo 'end of concert_db!';

 

?>

 

Here's what I get"  I see the echo to the screen of "end of concert_db", yet, I don't see the echo under the connection line.  Here's the output to the screen:

 

 

end of concert_db!

Fatal error: Call to undefined function: concert_connect() in /home/prefseat/public_html/trans-siberian-orchestra/trans-siberian-orchestra-winnipeg-mb-november-4.php on line 21

 

 

 

Any help would be appreciated!

 

Marcus

 

Link to comment
Share on other sites

I corrected the <?php tag.  Yet I have the same error.

 

Let me ask the question differently...

 

Do I have to define the $variable before I go to the require_once file? 

 

I program 14 languages and understand a little about scope, yet this seems different :(

 

mxl

Link to comment
Share on other sites

Post your actual code in trans-siberian-orchestra-winnipeg-mb-november-4.php up to and including line 21 mentioned in the error message. xxxxxx out any sensitive information but don't change the syntax used and don't remove any lines.

 

To help you with what your code is doing, we need to see the relevant code.

Link to comment
Share on other sites

Including or requiring files has nothing to do with variable scope. When you include/require it is as if the code in the included/require file was always present in the file that is doing the including/requiring.

 

 

My reference to his variable scope problems was his use of variables in the function being used outside of the function.

 

 

 

 

Can we see the entire code?  I'm not sure why it doesn't think concert_db is defined....

Link to comment
Share on other sites

Except for a couple of lines of concatenated strings, that WAS the code!

 

With your encouragement though, I made it through the problem...This is surely a mistake I'll never make again!  ;D I used 'http://myDomain.com/php/concert_db.php instead of the relational path.

 

Right now, the page at least builds, but the contents of $result don't seem to be making it back to the calling php page.

 

I think I have to define global variables to make it out of the function.

 

mxl

 

 

 

 

Link to comment
Share on other sites

I used 'http://myDomain.com/php/concert_db.php instead of the relational path.

 

When you don't post what you are actually doing, it takes a really long time to solve problems because the statement of the problem is no longer correct and 90% of the time, problems in code are due to what is actually being done in the code vs what should be done in the code.

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.