Mail-function not working (using mysqli output)

I’m having a problem: the mail function doesn’t work. My best guess is that there’s something wrong in one of the three queries i’ve used, or that I haven’t fetched the data correctly. See commentary in the code for further explanation. Thanks in advance.

$q1 = mysqli_query($conn, " // selects list and loot columns from table participants
						SELECT
									list, loot
						FROM
									participants
					");    
$index = 0; 
while($row = mysqli_fetch_assoc($q1)){  // loop to store the data in an associative array.
$loot[$index] = $row; // this is where $loot comes from
$index++;
}
for($i=1; $i<$result2[0]; $i++){ // $result2[0] is present in my code, i just didn't include it here as this worked fine
$q2 = mysqli_fetch_array(mysqli_query($conn, " // selects the email adresses of all participants, one by one
						SELECT
									emailadress
						FROM
									participants
          WHERE
                name = `'.$loot[$i]['loot'] . '`
					"));

$q3 = mysqli_fetch_array(mysqli_query($conn, " // selects the names of the participants, one by one
        	SELECT
        				name
        	FROM
        				participants
          WHERE
                emailadress = `'. $q2[0] .'`
        						"));

mail(`'. $q2[0] .'`, "subject", "Hi `'. $q3[0] .'`, The list of your assigned person is: `'. $loot[$i]['list'] .'`  greetings, the mailbot", NULL, "[email protected]"); // mails the participants their assigned lists and loot
}

There’s no guessing in programming. You must find out exactly why something isn’t working before you can fix what’s causing the problem.

You must ALWAYS have php’s error_reporting set to E_ALL. When learning, developing, and debugging code/queries, you should have display_errors set to ON. When on a live/public server, you should have display_errors set to OFF and log_errors set to ON. These settings should be in the php.ini on your system.

You must ALWAYS have error handling for statements that can fail, i.e. database and mail statements in the posted code. For database statements, the easiest error handling is to use exceptions for errors and in most cases let php catch the exception, where it will use its error related settings (see the above paragraph) to control what happens with the the actual error information (database errors will ‘automatically’ get display or logged the same as php errors.)

Next, this code is filled with incorrect back-tick usage and unnecessary queries. Back-ticks are used in an sql context when an identifier (database, table, or column name) requires special handling and should be avoided even then by using identifier names that don’t require them. You are incorrectly using back-tacks around data values, which will be producing sql query errors (see the above paragraph) about non-existent columns with names the same as the data values.

You should be using ONE query that gets the data you want, in the order that you want it, then simply loop over the data from the query to produce the email message body and call the mail() statement.

2 Likes

I do totally agree with phdr. But i can imagine that you are learning and that it will cost time. So if you want to see errors about what goes wrong when the database server tries to handle your query (which you can see like some kind of command) then you should take a closer look to the official documentation about the php build-in functions that you are using like for example mysqli_query(). If you google on “php mysqli_query” then you will find a direct link to the php.net’s page that explains everything about this function. If you then scroll down to the section Return Values you can read about the possible values that this function can return. The first sentence is the most important:

Returns FALSE on failure.

So that means that you can test if this function returns a false with:

if(mysqli_query($conn, $someQuery) === false) {
    // some error occured..
}

Next you have to know that there is also a mysqli_error() function that you can use to get some explanation about the error that occurred.

A small hint about the mysqli_ functions on php.net: look at the Procedural style examples when you are a beginner and are not common with object oriented programming.

To use exceptions for mysqli statement errors, add the following line of code before where you make the database connection -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

You now have simple, free, automatic, error handling for all the mysqli statements that can fail - connection, query, prepare, and execute, without adding any logic to the code, that you would need to change/remove when moving the code to a live/public server. You have also eliminated follow-on errors that are due to code continuing to run after the main error, since code execution transfers to the nearest exception handler upon an error, which would be php for the suggested usage in this thread.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service