无法重新声明的解决方法?
我刚刚完成此脚本的工作,我需要在同一页面中多次使用它。但是,当我第二次使用它时,我收到错误 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个丑陋的黑客,但你可以把它放在函数周围:
当然更好的方法是
include_once("includefile.php");
,其中includefile.php
包含有问题的函数(注意:includefile.php
还需要开始和结束标记)。
It's an ugly hack but you can put this around the function:
The better way of course is to
include_once("includefile.php");
, withincludefile.php
containing the function in question (note:includefile.php
also needs the opening and closing<?php ?>
tags).您可以将函数包装在
if(!function_exists('get_names'))
中,或者使用include_once
或require_once
来包含该文件而不是 < code>include 或修复您的调用脚本,使其不包含两次(或更多次)。You can wrap the function in
if(!function_exists('get_names'))
, or useinclude_once
orrequire_once
to include the file rather thaninclude
or fix your calling script to not include it twice (or more).将函数定义放入其自己的文件中,并在该文件的页面顶部调用一次 include(或 require)。
get_names 被定义多次,因此第二次引发致命错误。
编辑:
需要位于其自己的文件中的唯一部分是函数定义:
尽管我和其他人肯定会提倡进一步组织代码,但如果您将该函数放在像库这样的文件中。 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:
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.你可以尝试:
You could try: