遇到无法重新申报的问题
我刚刚写完这个脚本并让它工作,但我需要在一页上总共使用它 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 getNames 函数放入 afile.php 中并在开始时包含一次。从当前文件中取出 getNames 函数。
所以在第一个 php 文件中事情是这样的
(比如说)你有这个代码 - filea.php
你的第二个文件将有 fileb.php
现在只需按正常方式包含
或只是包含将您的代码包装在函数中并调用它
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
you second file will have fileb.php
Now just include as per normal
or just wrap your code in a function and call that instead
如果您使用此脚本 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.