我有this代码,但是当我尝试执行它时,它会出现以下错误:
Fatal error: Cannot redeclare genereerLiveCodeP() (previously declared in livestream.php:33) in livestream.php on line 32.
session_start();
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//header("location: index.php");
if($_SESSION['***'] == '***'){
$fp = fopen("test.html", 'w');
fwrite($fp, "");
fwrite($fp, '<p class="green">*** is online</p>');
$result = mysql_query("select count(1) FROM ***");
$row = mysql_fetch_array($result);
$rows = $row[0]+1000;
echo "Rows: ".$rows."\n";
for ($id = 1000; $id < $rows; $id++) {
echo "ID: ".$id."\n";
function genereerLiveCodeP () {
$lengthCode = 6;
$characters = '1234567890abcdefghijklmnopqrstuvwxyz';
$liveCodeFunction = '';
for ($p = 0; $p < $lengthCode; $p++) {
$liveCodeFunction .= $characters[mt_rand(0, strlen($characters))];
}
return $liveCodeFunction;
}
$livecode = genereerLiveCodeP ();
echo "Livecode: ".$livecode."\n";
$x = mysql_query("UPDATE *** SET livecode='".$livecode."' WHERE *** = '".$***."'");
echo $x."\n";
}
}
我该怎么办?
答案 0 :(得分:1)
在宣布for
之前,您忘了关闭function
循环。您的代码应如下所示:
...
for ($id = 1000; $id < $rows; $id++) {
echo "ID: ".$id."\n";
}
function genereerLiveCodeP () {
...
答案 1 :(得分:1)
首先,您使用的是已弃用的扩展程序(ext/mysql
)。
您需要将您的功能移到for
循环之外。 PHP无法正常工作(无法重新声明函数,因此错误)
如果你使用准备好的查询,你可以获得更大的性能提升,并且有更多面向未来的代码(当这些函数开始抛出错误时,你的代码将在PHP 5.5中中断)
session_start();
$db = new mysqli($host, $username, $password, $db_name);
function generate_live_code($length = 6) {
$characters = '1234567890abcdefghijklmnopqrstuvwxyz';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= $characters[mt_rand(0, strlen($characters))];
}
return $str;
}
//header("location: index.php");
if($_SESSION['id'] == 'debug') {
$fp = fopen("test.html", 'w');
fwrite($fp, "");
fwrite($fp, '<p class="green">*** is online</p>');
// writing html to a file? consider using a database...
$result = $db->query("select count(1) FROM x");
$row = $result->fetch_assoc($result);
$rows = $row[0]+1000;
echo "Rows: $rows\n"; // no need to concat with double quotes.
if ($query = $db->prepare("UPDATE x SET livecode = ? WHERE id = ?")) {
for ($id = 1000; $id < $rows; $id++) {
echo "ID: ".$id."\n";
$livecode = generate_live_code();
echo "Livecode: $livecode\n";
$query->bind_param("si", $livecode, $id);
$query->execute();
}
}
}
答案 2 :(得分:0)
您在循环中声明函数genereerLiveCodeP()
,尝试将其放在文件的开头。