php Show the variable

Asked

Viewed 515 times

4

I have a page that sends emails when one of my form dates is ending. Currently he only sends the name of the person who has the date to finish but I want him 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];(...)
  • 1

    theoretically it is only that. To know the chaves of the array, of a while($row = mysql_fetch_array($validade)){ echo '<pre>'; print_r($row);}

  • You want to know how to find the date that is to end or how to display the date?

  • I just want to show the date you are finishing the person $Name

2 answers

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>";
  • $dataTermino = $Row['data2']; But if I have more dates? put: $dataTermino1 = $Row['data3']; $dataTermino2 = $Row['data4']; (...)

  • 1

    Or you can put the $row['data3'] straight into the string. No need to keep creating variables.

3


According to your select, the equivalent indices 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 the mysql_fetch_array we can pass a second optional parameter, the result_type which may be the constant MYSQL_BOTH, MYSQL_NUM or MYSQL_ASSOC. If not reported, the value used by default by the function is the 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 value of results, using the standard you double the amount of results stored in the server memory.

These are the little things that influence the performance of your script/application.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.