Unanswered PHP in fetch command

Asked

Viewed 99 times

2

I am studying with a book use the head and the following I practically copied and pasted the sample code just changing the names of the variables and I am not having the return with my code, in fact what appears in my final result is email sent to: 3x this because I have 3 entries in my database, but the values are not being placed inside the variables so the function is not working right, is not sending email or email return sent to: [email protected] Thanks in advance.

<?php
  $de = '[email protected]';
  $assunto = $_POST['assunto'];
  $texto = $_POST['elvismail'];

  /*creating connection*/
  $conexao = mysqli_connect('localhost', 'root', 'password', 'ELVIS_STORE')
  or die ('Erro ao conectar o banco');  

  /*command SQL*/
  $query = "SELECT * FROM LISTA_EMAIL"; 

  /*getting result*/
  $result = mysqli_query($conexao, $query) or die ('erro na segunda etapa');   

  while ($row = mysqli_fetch_array($result))
 {
   $para = $row['email'];
   $nome = $row['nome'];
   $sobrenome = $row['nome'];
   mail($para, $assunto, $texto, 'De: ' . $de);
   echo 'Email sent to: ' . $para . '<br />';
 }
  mysqli_close($conexao);
?>

1 answer

0

First: table name is on CAPITAL? Second, are the connections correct? Third: The constant changes of PHP in relation to Mysql (depreciation of mysql_* in relation to mysqli_*) made me search and guide you to change, after SELECT, the code snippet like this:

if ($stmt = mysqli_prepare($link, $query)) {

/* execute statement */
mysqli_stmt_execute($stmt);

/* bind result variables */
mysqli_stmt_bind_result($stmt, $name, $code);

/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
    printf ("%s (%s)\n", $name, $code);
}

/* close statement */
mysqli_stmt_close($stmt);
}

I hope I’ve helped.

  • Thank you Willian, about the issue of the bank being capitalized yes it is and this was not a problem because before removing the data from the database I created a form to send them and everything worked out (even using capital letters). about the connections I used the same procedure of the previous form to use connection or it is the same way I created the connection for sending I used to remove and the connection is ok (so much so that the browser delivers me the message "email sent to:" 3x the detail is that after the to: should appear the email of registered people, which by the way are 3 and does not appear.

  • about the depreciation I did not understand you recommend that I continue using mysqli or switch to mysql (_fetch) and about your code... then it became very difficult for me to understand I am at the beginning and what is happening seems basic, But I haven’t really figured out what it is yet and the book just shows that. Thanks for everything anyway.

  • The books are outdated, especially this series "Use the Head". This code snippet I took from php’s own documentation. I recommend you access: (http://php.net/manual/en/mysqli-stmt.fetch.php). On the question of not appearing, give a print_r($teuArray) before While to check what its format.

  • Hi William this is the print_r($meuarray) result mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 3 [type] => 0 )

  • That’s the print_r($result)?? Seems like print_r(mysqli_query). No bank data, just information.

  • Exactly and this is where it lives what I am not understanding pq created variable result says that it is equal to mysqli_query($connected, $query) otherwise show me the error msg (at least that’s what I understood from the code $result = mysqli_query($connected, $query) or die ('error in the second step'); and when I gave the print_r ($result) it is this data that she brings me I think that if she brought the data from inside the bank even the problem would be solved.

  • But the result will bring that right, what you have to put is the $Row. print_r($row). This before the while. give an Exit() after.

  • 1

    William thanks for your attention already solved here, in fact it was all right the function the formula up there is all ok the detail is that inside the array While the variable $Row ['email']; it should be written $Row['EMAIL'] because that’s how I wrote in the database, I made that change for the others and it worked out. Moving

Show 3 more comments

Browser other questions tagged

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