Scripts and tips for debugging code in PHP, MySQL connection and maïl()

When you look for the bug, you find it.

Carte mère d'ordinateur. Crédit photo: Gigabyte

Enable PHP error reporting

You will get an extremely detailed error log with the contents of all variables. Paste the above code at the beginning of your script, just after “<?php”

Enabling PHP error reporting
<?php 
error_reporting(E_ALL);   // Enable reporting of PHP errors. You can only use this line, it will already give a lot of details.

$variables = get_defined_vars(); // Gives the contents and values of all variables in the current scope
$var_ignore=array("GLOBALS", "_ENV", "_SERVER"); // Determines the vars not to be displayed
echo ("<strong>Status of the variables on the line : ".__LINE__." dans le fichier : ".__FILE__."</strong><br />\n");
$nom_fonction=__FUNCTION__;
if (isset($nom_fonction)&&$nom_fonction!="") {
  echo ("<strong>In the function : ".$nom_fonction."</strong><br />\n");
}
foreach ($variables as $key=>$value) {
  if (!in_array($key, $var_ignore)&&strpos($key,"HTTP")===false) {
    echo "<pre>";
    echo ("$".$key." => ");
    print_r($value);
    echo "</pre>\n";
  }
}  

Check the connection to its MySQL database

Create a file that you will call for example debug_mysql.php, then upload it to your server. Fill in your MySQL database connection parameters. If the script says that there is an entry and that it contains "Anne NONYME", then you have no connection problem.

You can take this PHP file here: sql-test-en.zip (1.5 KB)

Debugging MySQL database connection:
<?php
/* By "Abogil", create a file debug.php */ 

error_reporting(E_ALL);	// Enable PHP error reporting 

// ****** Configuration Start ****** 
 $DBhost = "server"; // SQL server address 
 $DBName = "name"; // SQL database name 
 $DBowner= "login"; // login SQL 
 $DBpw	 = "pwd"; // password SQL 
// ****** Configuration End ****** 
	 
$fh_db = mysqli_connect($DBhost, $DBowner, $DBpw, $DBName); 
$Erreur = mysqli_connect_error($fh_db); 
if (strlen($Erreur) > 0) { echo "1- mysqli_connect Connect to DATABASE = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "1- mysqli_connect Connect to DATABASE = <strong> OK </strong> <br /> <br />\n"; }

$sel = mysqli_select_db($fh_db, $DBName); 
$Erreur = mysqli_error($sel); 
if (strlen($Erreur) > 0) { echo "2- mysqli_select_db Access to DATABASE $DBName = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "2- mysqli_select_db Access to DATABASE $DBName = <strong> OK </strong> <br /> <br />\n"; }

// Creating table Tab_test_DB 
$sql_query="CREATE TABLE `Tab_test_DB` ( `Id` int(11) NOT NULL auto_increment, `NOM` varchar(32) NOT NULL default '', `PRENOM` varchar(32) NOT NULL default '', PRIMARY KEY (`Id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";	 // Query 
$result_query=mysqli_query($fh_db, $sql_query);	 // Query execution 
$Erreur = mysqli_error(); 
echo "3- \$sql_query=<strong>$sql_query</strong> <br /> \$result_query=<strong>" . $result_query . "</strong> <br /> \n"; 
if (strlen($Erreur) > 0) { echo "Creating table Tab_test_DB = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "Creating table Tab_test_DB = <strong> OK </strong> <br /> <br />\n"; }

// Adding a record 
$sql_query="INSERT INTO `Tab_test_DB` values ('','NONYME','Anne')";	 // Query 
$result_query=mysqli_query($fh_db, $sql_query);	 // Query execution 
$Erreur = mysqli_error(); 
echo "4- \$sql_query=<strong>$sql_query</strong> <br /> \$result_query=<strong>" . $result_query . "</strong> <br /> \n"; 
if (strlen($Erreur) > 0) { echo "Adding a record = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "Adding a record = <strong> OK </strong> <br /> <br />\n"; }

// SELECT in table Tab_test_DB 
$sql_query="SELECT Id, NOM, PRENOM FROM `Tab_test_DB` WHERE NOM=\'NONYME\'";	 // Query 
$result_query=mysqli_query($fh_db, $sql_query);	 // Query execution 
$Erreur = mysqli_error(); 
echo "5- \$sql_query=<strong>$sql_query</strong> <br /> \n"; 
if (strlen($Erreur) > 0) { echo "SELECT in table Tab_test_DB = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "SELECT in table Tab_test_DB = <strong> OK </strong> <br /> \n"; }

$sql_query="SELECT Id, NOM, PRENOM FROM `Tab_test_DB` ORDER BY NOM";	 // Query 
$result_query=mysqli_query($fh_db, $sql_query);	 // Query execution 
$result_count_row_get = mysqli_num_rows($result_query);	
$Erreur = mysqli_error(); 
echo "Number of results found = <strong>$result_count_row_get</strong> <br /> <br /> \n"; 
if (strlen($Erreur) > 0)	exit; 
mysqli_free_result($result_query);

// Removal of the table Tab_test_DB 
$sql_query="DROP TABLE IF EXISTS `Tab_test_DB`";	 // Query 
$result_query=mysqli_query($fh_db, $sql_query);	 // Query execution 
$Erreur = mysqli_error(); 
echo "6- \$sql_query=<strong>$sql_query</strong> <br /> \$result_query=<strong>" . $result_query . "</strong> <br /> \n"; 
if (strlen($Erreur) > 0) { echo "Removal of the table Tab_test_DB = <strong>" . $Erreur . "</strong> <br /> <br />\n"; exit; } 
else { echo "Removal of the table Tab_test_DB = <strong> OK </strong> <br /> <br />\n"; }

// Closing connection 
mysqli_close($fh_db); 
?>

Testing the sending of messages via mail() function

Sometimes the messages don't come through. Either they are not sent because the server is down, or they are lost somewhere, or the script is badly programmed. Here is a PHP script that works, it will help you test the situation. Create a file message.php and copy the code below.

You can get this PHP file here: courrier-test-en.zip (1.1 KB)

Test mail() function
<?php
// TEST FUNCTION MAIL() PHP
// CREATE A FILE email.php

// *** To setup
$to = "adresse@machin.truc";  
$from  = "postmaster@bidule.truc";  

// *** Leave as is
$jour  = date("d-m-Y");
$heure = date("H:i");

$sujet = "Test Mail $jour $heure";

$contenu = "";
$contenu .= "<html> \n";
$contenu .= "<head> \n";
$contenu .= "<title> Subject </title> \n";
$contenu .= "</head> \n";
$contenu .= "<body> \n";
$contenu .= "Mail in simple HTML format with the PHP mail().<br> <b>$sujet </b> <br> \n";
$contenu .= "</body> \n";
$contenu .= "</HTML> \n";

$headers  = "MIME-Version: 1.0 \n";
$headers .= "Content-Transfer-Encoding: 8bit \n";
$headers .= "Content-type: text/html; charset=utf-8 \n";
$headers .= "From: $from  \n";
// $headers .= "Disposition-Notification-To: $from  \n"; // acknowledgement of receipt

$verif_envoi_mail = TRUE;

$verif_envoi_mail = @mail ($to, $sujet, $contenu, $headers);
 
if ($verif_envoi_mail === FALSE) echo " ### Verification Sending Mail=$verif_envoi_mail Error sending mail <br> \n";
else echo " *** Verification Sending Mail=$verif_envoi_mail Mail sent successfully to $to from $from <br> with the subject: $sujet \n"; 
?>

“If-” you'll be a Man…

Re-discover Rudyard Kipling's famous poem “If- you'll be a Man, my son” which has inspired so many generations. It celebrates the courage to overcome hardship.

I've decided to succeed

I have put together 10 tried and tested tips for finding the will to successfully achieve your ambitions and overcome obstacles in your personal and professional life.

Gourmet treats

Exquisite recipes for mini-cakes (madeleines, financiers, biscuits, cakes, muffins) and other delicacies (croissants, brioche, traditional cakes…).