无法重新声明的解决方法?

发布于 2025-01-04 21:01:02 字数 1044 浏览 4 评论 0原文

我刚刚完成此脚本的工作,我需要在同一页面中多次使用它。但是,当我第二次使用它时,我收到错误 Fatal error: Cannot redeclare get_names() (previously returned)。我四处寻找答案,但我能找到的只是使用一次命令,但它似乎不起作用 是脚本:

<?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;

这 ?>

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;

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流心雨 2025-01-11 21:01:02

这是一个丑陋的黑客,但你可以把它放在函数周围:

if ( !function_exists("get_names") ) {

/// the function

}

当然更好的方法是 include_once("includefile.php");,其中 includefile.php 包含有问题的函数(注意:includefile.php 还需要开始和结束 标记)。

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

错々过的事 2025-01-11 21:01:02

您可以将函数包装在 if(!function_exists('get_names')) 中,或者使用 include_oncerequire_once 来包含该文件而不是 < code>include 或修复您的调用脚本,使其不包含两次(或更多次)。

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

天涯离梦残月幽梦 2025-01-11 21:01:02

将函数定义放入其自己的文件中,并在该文件的页面顶部调用一次 include(或 require)。

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

尽管我和其他人肯定会提倡进一步组织代码,但如果您将该函数放在像库这样的文件中。 php 或其他东西,然后 require_once('path/to/library.php') 它,您应该能够多次运行其余部分。

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.

最美的太阳 2025-01-11 21:01:02

你可以尝试:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文