Define a constant function call

My code looks like this:

<?php
define("ERROR", "SOMETHING WRONG WITH MY DATABASE");
... 
if (!mysql_query($q)){
  die(ERROR);
}
?>

Now I want to replace "SOMETHING WRONG WITH MY DATABASE" with mysql_error()in case I want to debug it. what is the easiest way?

This does not work: define("ERROR", mysql_error());

---- edit ---

I do not want to use mysql_error () in a production environment, can this help an attacker figure out something related to my database? What is my constant string usage point

like in C you can do #define x yourfunction() I'm not sure if I can do the same in php

0
source share
4 answers

: " ". , , . , - .

, , - , eval , - :

define("ERROR", "return mysql_error()");
...
die(eval(ERROR));

.

die(mysql_error());
+3

, - . , , , - - .

- :

<?php

  define("ERROR", "SOMETHING WRONG WITH MY DATABASE: ");

  // ... 

  if (!mysql_query($q)){
    die(ERROR . mysql_error());
  }

?>
+1

mysql_error() sql, . . , , , :

die(mysql_error());

define("ERROR", "Database Problem ");

function Err() {
  $sql_err = ERROR . mysql_error();
  return $sql_err;
}
// sql operation here, then test for error
die(Err());
+1

- DEBUG, prod dev:

define('DEBUG', 1);

// In the function:
// ...
echo ERROR;

if (DEBUG){
  echo mysql_error();   
}

die();
0

Source: https://habr.com/ru/post/1533567/


All Articles