php Show the variable

4

I have a page that sends emails when one of the dates on my form is ending. Currently it only sends the name of the person who has the date to end but I want it to inform the date that is at the end.

sql = ("SELECT Nome, data1, data2, data3, data4 < (now()+ interval 10 day)");
$validade = mysql_query($sql);

while($row = mysql_fetch_array($validade)){
$Nome = $row[0];
$PHPMailer = new PHPMailer(); 
(....)
$PHPMailer->Body = "<body><p><strong>Faltam 10 dias para terminar</strong> $Nome</body>";

Just put the dates like this?

$data1 = $row[1];
$data2 = $row[2];(...)
    
asked by anonymous 13.02.2014 / 10:16

2 answers

3

According to your select , equivalent indexes are:

  • 0 equivalent to Name
  • 1 equivalent to data1
  • 2 equivalent to data2
  • 3 equivalent to data3
  • 4 equivalent to data4

Assuming the connection to the bank is already established. The code would be:

sql = ("SELECT Nome, data1, data2, data3, data4 < (now()+ interval 10 day)");
$validade = mysql_query($sql);

while($row = mysql_fetch_array($validade))
{
    $nome = $row[0];
    $prazo = $row[1]; // Ou o índice da data que desejar acessar
    $mensagem = "<body><p><strong>Faltam menos de 10 dias para terminar $Nome.</strong></p>" .
                "<p>Prazo: $prazo</p></body>";

    $PHPMailer = new PHPMailer(); 
    (....)
    $PHPMailer->Body = $mensagem;
}

If you prefer to use the field name to capture the data, do:

$nome = $row['Nome'];
$prazo = $row['data1']; // Ou o nome do campo da data que desejar acessar

Remembering that when we use mysql_fetch_array we can pass an optional second parameter, result_type which can be the MYSQL_BOTH , MYSQL_NUM or MYSQL_ASSOC . If not informed, the value used by default by the function is MYSQL_BOTH .

Examples of how your result would come (I used random values):

// Usando MYSQL_BOTH
array (size=10)
    0 => string 'nome capturado' (length=14)
    1 => string '2013-12-20' (length=10)
    2 => string '2014-01-20' (length=10)
    3 => string '2014-02-20' (length=10)
    4 => string '2014-03-20' (length=10)
    'Nome'  => string 'nome capturado' (length=14)
    'data1' => string '2013-12-20' (length=10)
    'data2' => string '2014-01-20' (length=10)
    'data3' => string '2014-02-20' (length=10)
    'data4' => string '2014-03-20' (length=10)

// Usando MYSQL_NUM
array (size=10)
    0 => string 'nome capturado' (length=14)
    1 => string '2013-12-20' (length=10)
    2 => string '2014-01-20' (length=10)
    3 => string '2014-02-20' (length=10)
    4 => string '2014-03-20' (length=10)

// Usando MYSQL_ASSOC
array (size=10)
    'Nome'  => string 'nome capturado' (length=14)
    'data1' => string '2013-12-20' (length=10)
    'data2' => string '2014-01-20' (length=10)
    'data3' => string '2014-02-20' (length=10)
    'data4' => string '2014-03-20' (length=10)

Imagine that you return a high result value, by default you double the amount of results stored in server memory.

It's these little things that influence the performance of your script / application.

To further optimize your script, I advise you to use PDO to access the database of data. To work with dates in PHP, use the class DateTime that has several Date / Time specific functions .

    
13.02.2014 / 13:10
3

See if this can solve your problem.

sql = ("SELECT Nome, data1, data2, data3, data4 < (now()+ interval 10 day)");
$validade = mysql_query($sql);

while($row = mysql_fetch_array($validade)){
$Nome = $row[0];
$dataTermino = $row['data2']; // Ou a data que você deseja
$PHPMailer = new PHPMailer(); 
(....)
$PHPMailer->Body = "<body><p><strong>Faltam 10 dias para terminar</strong>$Nome</body>";
$PHPMailer->Body = "<body><p><strong>A data prevista é</strong>$dataTermino</body>";
    
13.02.2014 / 11:42