数组仅插入一行

时间:2018-12-03 12:34:00

标签: php mysql

我正在尝试使用php在excel中导出数据库。但是while循环仅在数组中插入一行值,并且导出的excel文件将仅包含一行数据。

我正在使用嵌套循环进行表回调,以插入不是不为空的数据。因此,基本上有两个值数据库被导出到一个excel文件中。首先,将数据存储在数组中,然后将其导出为ex​​cel文件。

这是代码:

   $sql = "SELECT * FROM leads";
   $result = mysqli_query($conn, $sql);
   $data = array();
    while ($row1 = mysqli_fetch_array($result))
    {
    if ($row1["status"] == "1") 
    {
    $status = "New";
    }
    if ($row1["status"] == "2") 
    {
    $status = "Assigned";
    }
    if ($row1["status"] == "3") 
    {
    $status = "Pending";
    }
    if ($row1["status"] == "4") 
    {
    $status = "Closed";
    }
    if ($row1["status"] == "5") 
    {
    $status = "Denied";
    }
    $id = $row1["id"];
    $sql2 = "SELECT * FROM callback WHERE lead_id=$id ORDER BY id DESC LIMIT 1";
   $result2 = mysqli_query($conn, $sql2);
    if ($result2)
    {
    while ($row2 = mysqli_fetch_array($result2))
    {


    $data[] = array(
    "Created" => $row1["curdate"], 
    "Customer Name" => $row1["cname"], 
    "Designation" => $row1["designation"],
    "Contact" => $row1["number"],
    "Company" => $row1["company"],
    "Address" => $row1["street"],
    "Landmark" => $row1["landmark"],
    "Zip" => $row1["zip"],
    "Products" => $row1["product"],
    "Last Callback" => $row2["callback"],
    "Last Visit" => $row2["vdate"],
    "Status" => $status
   );
   }
   }
   else
   {
    $data[] = array(
   "Created" => $row1["curdate"], 
    "Customer Name" => $row1["cname"], 
    "Designation" => $row1["designation"],
    "Contact" => $row1["number"],
    "Company" => $row1["company"],
    "Address" => $row1["street"],
    "Landmark" => $row1["landmark"],
    "Zip" => $row1["zip"],
    "Products" => $row1["product"],
    "Last Callback" => "NULL",
    "Last Visit" => "NULL",
    "Status" => $status
    );
    }
    }

我在做什么错?我也是这个菜鸟,所以请对我轻松一点。

1 个答案:

答案 0 :(得分:1)

$result2mysqli_result对象,您不能在if语句中使用它。您应该改用其num_rows属性,即

if ($result2->num_rows > 0) 

(代替if ($result2)