2

I just finished getting this script working and I need to use it multiple times in the same page. However, when I use it a second time I get the error Fatal error: Cannot redeclare get_names() (previously declared. I looked around for an answer but all I could find was to use the once command but it doesn't seem to work with get. Here is the script:

<?php
$db = mysql_connect('localhost', 'username', 'pass') or die("Database error");
mysql_select_db('dbname', $db);

$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
if ($row['pool'] % 2) {
    echo "<h4>Result 1</h4>";
    $names = get_names(1);
    foreach($names as $name) {
        echo $name . "<br/>";
    }
} else {
    echo "<h4>Result 2</h4>";
    $names = get_names(0);
    foreach($names as $name) {
        echo $name . "<br/>";
    }
}

function get_names($pool_result)
{
$name_array = array();

$query = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    array_push($name_array, $row['name']);
}

return $name_array;

} ?>

user1206214
  • 63
  • 1
  • 7

4 Answers4

2

It's an ugly hack but you can put this around the function:

if ( !function_exists("get_names") ) {

/// the function

}

The better way of course is to include_once("includefile.php");, with includefile.php containing the function in question (note: includefile.php also needs the opening and closing <?php ?> tags).

mvds
  • 45,755
  • 8
  • 102
  • 111
  • I just tried this and I'm still getting the same error. What parts exactly need to be put into the php file that I'm using include_once on? – user1206214 Feb 14 '12 at 03:41
  • the `function get_names($pool_result) { ... }` part. Or you could put that function in the file that is apparently including the script multiple times. – mvds Feb 14 '12 at 10:23
2

You can wrap the function in if(!function_exists('get_names')), or use include_once or require_once to include the file rather than include or fix your calling script to not include it twice (or more).

mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

Put the function definition into it's own file and call include (or require) once at the top of the page on that file.

get_names is getting defined more than once and so the second time raising a fatal error.

EDIT:

The only part that needs to be in it's own file is the function definition:

function get_names($pool_result)
{
    $name_array = array();

    $query  = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result))
        array_push($name_array, $row['name']);

    return $name_array;
}

Though I and surely others would advocate further organization of the code, if you put that function in a file like, library.php or something and then require_once('path/to/library.php') it, you should be able to run the rest of it multiple times.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • I just tried this and I'm still getting the same error. What parts exactly need to be put into the php file that I'm using include_once on? – user1206214 Feb 14 '12 at 03:40
0

You could try:

<?php
$db = mysql_connect('localhost', 'username', 'pass') or die("Database error");
mysql_select_db('dbname', $db);

$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
if ($row['pool'] % 2) {
    echo "<h4>Result 1</h4>";
    $names = get_names(1);
    foreach($names as $name) {
        echo $name . "<br/>";
    }
} else {
    echo "<h4>Result 2</h4>";
    $names = get_names(0);
    foreach($names as $name) {
        echo $name . "<br/>";
    }
}
if(!function_exists("get_names")) {

  function get_names($pool_result)
  {
  $name_array = array();

  $query = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
  $result = mysql_query($query);

  while ($row = mysql_fetch_array($result)) {
      array_push($name_array, $row['name']);
  }

  return $name_array;
}
broesch
  • 2,442
  • 1
  • 19
  • 9