将数据库结果数组转换为变量php

时间:2018-04-25 09:10:23

标签: php mysql

我正在尝试将数据库数组转换为变量以插入到另一个函数中,但我一直在获取未定义的变量'。如果有人能引导我朝着正确的方向前进,我将不胜感激。

$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);

3 个答案:

答案 0 :(得分:1)

如果你需要做的是将数据库结果行提取到具有相应名称的变量,你可以使用php的“extract”方法。

<xsl:variable name="NumFields" select="count(preceding-sibling::table/tr[1]/td[.=''])" />

答案 1 :(得分:0)

您的代码不正确。将数组元素分配给变量应按如下方式进行:

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);

}

答案 2 :(得分:0)

您的变量分配是错误的,在PHP中,您可以像这样为变量赋值:

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

所以你的代码必须是这样的:

<?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);
?>

更快的方法,你甚至不需要变量:

<?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);
?>