1

I have this code, but when I try to execute it, it gives the following error:
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";

}



}

What should I do?

Amelia
  • 2,967
  • 2
  • 24
  • 39
Lukas Goes
  • 45
  • 10
  • 1
    for a start, what you should do: [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. In addition, **escape/prepare your SQL** – Amelia Feb 09 '13 at 22:23
  • 1
    Thank you. I will update my code soon (before I launch the website). – Lukas Goes Feb 09 '13 at 22:25

3 Answers3

1

You forgot to close the for loop before declaring the function. Your code should look like this:

...
for ($id = 1000; $id < $rows; $id++) {
echo "ID: ".$id."\n";
}

function genereerLiveCodeP () {
...
1

Firstly, you are using a deprecated extension (ext/mysql).

You need to move your function outside of the for loop. PHP doesn't work that way (redeclaring a function isn't possible, hence the error)

You can get a bigger boost in performance if you use a prepared query, and have far more future-proof code (your code will break in PHP 5.5 when those functions start throwing errors)

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();
        }
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Amelia
  • 2,967
  • 2
  • 24
  • 39
0

You are declaring function genereerLiveCodeP() inside a loop, try to put it at the beginning of the file.

Peter Krejci
  • 3,182
  • 6
  • 31
  • 49