Monday, 27 August 2018

My php generated email does not show all records in my database

I don't know what is this not working the way I want it.. I have a database with a few records and the is query showing only the first row, instead of all.

<?php
$con=mysqli_connect("localhost"," "," "," ");
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM programari");
$to = "my_email@yahoo.com";
$subject = "testing mail";

while($row = mysqli_fetch_array($result))
  {
   $body= $row['cand'];
  }

if (mail($to, $subject, $body)) {
echo("<p>Email successfully sent!</p>");
} else {
echo("<p>Email delivery failed…</p>");
}

?>


You are accidentally assigning to $body instead of concatenating. Short and sweet:
$body .= $row['cand'];

Edit: you should also initialize $body to an empty string before your loop, to avoid a notice on the first run.

0 comments:

Post a Comment