1

I am trying to make database array into variables to insert into another function, but I keep getting 'Undefined variable'. I would greatly appreciate it if anybody can guide me in the right direction.

$sql = "SELECT * FROM $tablename WHERE start_day = '".$currentdate."'  ";
     $result = mysqli_query($conn,$sql);

     while($row = mysqli_fetch_array($result))
     {

       $row ['name'] = $name;
       $row ['email'] = $email;
       $row ['bID'] = $bID;
       $row ['start_time'] = $start_time;
       $row ['end_time'] = $end_time;
      $row ['start_day'] = $start_day;
    MailReminder::sendMail($name , $email, $bID, $start_time,$endtime    
  ,$start_day);



    }
mysqli_close($conn);
4
  • Which is your Undefined variable? Commented Apr 25, 2018 at 9:11
  • it says name, email, bID, start_time, end_time ,start_day Commented Apr 25, 2018 at 9:14
  • Swap the vars. $row ['name'] = $name; should be $name = $row ['name']; Commented Apr 25, 2018 at 9:18
  • 2
    This doesn’t need any “extra variables” ... you can just use $row['name'] etc. directly inside the method call. Commented Apr 25, 2018 at 9:19

3 Answers 3

1

If what you need to do is extract a database result row to variables with corresponding names you can use php's "extract" method.

<?php
while ($row = mysqli_fetch_array($result)) {
    extract($row)
    MailReminder::sendMail($name , $email, $bID, $start_time, $endtime, $start_day);
}
mysqli_close($conn);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is incorrect. Assigning elements of an array to variables should be done as below:

while($row = mysqli_fetch_array($result))
{

       $name = $row ['name'];
       $email = $row ['email'];
       $bID = $row ['bID'];
       $start_time = $row ['start_time'];
       $end_time = $row ['end_time'];
       $start_day = $row ['start_day'];

       MailReminder::sendMail($name , $email, $bID, $start_time,$endtime ,$start_day);

}

Comments

0

Your variable assignements are wrong, in PHP you assign a value to variable like this:

$myVariable = 'myValue'; // Correct
'myValue' = $myVariable; // Not correct, will not work

So your code must be like that:

<?php
while ($row = mysqli_fetch_array($result))
{
  $name = $row ['name'];
  $email = $row ['email'];
  $bID = $row ['bID'];
  $start_time = $row ['start_time'];
  $end_time = $row ['end_time'];
  $start_day = $row ['start_day'];
  MailReminder::sendMail($name , $email, $bID, $start_time, $endtime, $start_day);
}
mysqli_close($conn);
?>

Faster method where you don't even need the variables:

<?php
while ($row = mysqli_fetch_array($result))
{
  MailReminder::sendMail($row ['name'] , $row ['email'], $row ['bID'], $row ['start_time'], $row ['end_time'], $row ['start_day']);
}
mysqli_close($conn);
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.