"Fatal Error: Cannot Redeclare ≪Function≫"

Fatal error: Cannot redeclare function

This errors says your function is already defined ; which can mean :

  • you have the same function defined in two files
  • or you have the same function defined in two places in the same file
  • or the file in which your function is defined is included two times (so, it seems the function is defined two times)

To help with the third point, a solution would be to use include_once instead of include when including your functions.php file -- so it cannot be included more than once.

Cannot redeclare function php

You (or Joomla) is likely including this file multiple times. Enclose your function in a conditional block:

if (!function_exists('parseDate')) {
// ... proceed to declare your function
}

Laravel 8 Custom Helper function PHP Fatal error: Cannot redeclare functionName() previously declared in C:(patth)Helpers.php

You can bypass this error by checking if your function already exists:

if(! function_exists('CheckInvalidPlan')) {
function CheckInvalidPlan($id)
{
if (Plan::find($id) == null)
{
return true;
}
}
}

That's how Laravel helpers are declared:

if (! function_exists('today')) {
/**
* Create a new Carbon instance for the current date.
*
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function today($tz = null)
{
return Date::today($tz);
}
}

However, a cleaner approach would be to understand why your helpers file is loaded twice.

It is hard to tell you exacly where the error could be, however you should inspect all your classes, the app\Helpers.php file should never be required manually. It should be autoloaded by composer, as explained in this answer (thanks N69S).

PHP Cannot redeclare function, previously defined?

As per your comment, you are placing the function_exists check in the wrong place, change your code to the following:

if (!function_exists('checkIfAdminExists')) 
{
function checkIfAdminExists($username, $password) {
require_once("db_connection.php");

$sql = "SELECT personid, username, password FROM table_test";
$result = $dbcon->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["username"] == $username && $row["password"] == $password) {
return true;
}
}
} else {
return false;
}

$dbcon->close();
}
}

Now, with the above code, the function will only be defined if it doesn't already exist.

Note: Please stop using mysql_* as it has been officially deprecated and removed in PHP 7. It would be wise to start learning mysqli_* or PDO and to make use of prepared statements.

Update #1

You are setting the username variable twice:

$username = mysql_real_escape_string($username);
$username = mysql_real_escape_string($password);

It should be:

$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

Update #2

As per the documentation of mysql_real_escape_string, it takes an optional second parameter:

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

With the above in mind, it means that your connection has not been established. So the next logical question is: Where is your connection being established in create_admin.php?



Related Topics



Leave a reply



Submit