遇到无法重新申报的问题

发布于 2025-01-05 16:51:27 字数 1165 浏览 4 评论 0原文

我刚刚写完这个脚本并让它工作,但我需要在一页上总共使用它 8 次。第一次工作正常,但第二次我得到:致命错误无法重新声明 get_names()。有人告诉我解决这个问题的方法是使用 include_once 但我似乎无法弄清楚我到底应该如何做到这一点。我尝试将 get_names 部分从代码中删除,并将它们放入单独的 php 文件中,然后使用 include_once 命令。我让它工作了,但在尝试使用该脚本两次后,我再次遇到了同样的错误。我还尝试将整个脚本放入 php 文件中,然后使用 include_once("scriptname.php") 命令,但发生了同样的事情。所以我的问题是我到底如何剪切这个脚本,这样我就不会再收到这个错误了?

<?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 writing this script and getting it to work but I need to use it a total of 8 times on 1 page. It works fine the first time but the second time I get: Fatal Error cannot redeclare get_names(). I've been told the way around this is to use include_once but I can't seem to figure out how exactly I'm supposed to do that. I've tried cutting both of the get_names parts out of the code and putting them into separate php files then using the include_once command. I got it to work but once again I got the same error after trying to use the script twice. I also tried putting the whole script into a php file and then using the include_once("scriptname.php") command and the same thing happened. So my questiion is how exactly do I cut this script up so I don't get this error anymore?

<?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技术交流群

发布评论

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

评论(2

爱要勇敢去追 2025-01-12 16:51:27

将 getNames 函数放入 afile.php 中并在开始时包含一次。从当前文件中取出 getNames 函数。

所以在第一个 php 文件中事情是这样的

(比如说)你有这个代码 - filea.php

 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;

}

你的第二个文件将有 fileb.php

 $db = mysql_connect('localhost', 'username', 'pass') or die("Database error");
 ..... rest of source code excluded. Make sure you get rid of getNames in this file

现在只需按正常方式包含

include 'filea.php'; // include it once only


include 'fileb.php'; // as many times as your wish

或只是包含将您的代码包装在函数中并调用它

put getNames function in afile.php and include that once at the start. take getNames function out of your current file.

so things goes like this

in first php file (say) you have this code - filea.php

 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 second file will have fileb.php

 $db = mysql_connect('localhost', 'username', 'pass') or die("Database error");
 ..... rest of source code excluded. Make sure you get rid of getNames in this file

Now just include as per normal

include 'filea.php'; // include it once only


include 'fileb.php'; // as many times as your wish

or just wrap your code in a function and call that instead

菩提树下叶撕阳。 2025-01-12 16:51:27

如果您使用此脚本 8 次,则将整个脚本放入一个函数中并调用该函数 8 次,而不是复制粘贴。

If you are using this script 8 times, then put the entire thing inside a function and call that function 8 times instead of copy pasting.

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