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;
} ?>
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.
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).
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).
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;
}