从两个表中检索信息

时间:2016-02-21 01:50:59

标签: php mysql

我有一个名为incidents的表,我想从中检索4个字段/列,但我需要firstname表中的lastnamecustomer。我一直收到以下错误:

  

警告:为foreach()提供的参数无效

这是相关代码:

public static function get_incidents_unassigned(){
    $db = Database::getDB();
    $query = 'SELECT concat(customer.firstName,customer.lastName) AS name, incident.productCode, incident.dateOpened, incident.title, incident.description FROM incidents

            INNER JOIN customers
            ON incident.customerID = customer.customerID
            WHERE techID IS NULL';
    $statement = $db->prepare($query);
    $statement->execute();
    $rows = $statement->fetch();

    $incidentList = array();
    foreach ($rows as $row){    
        $incidents = new Incident(
                $row['name'], $row['productCode'],
                $row['dateOpened'], $row['title'], $row['description']);

        $incidentList[] = $incident;
    }
    $statement->closeCursor();
    return $incidentList;
}

******但这是涉及的表格

<table>
        <tr>
            <th>Customer</th>
            <th>Product</th>
            <th>Date Opened</th>
            <th>Title</th>
            <th>Description</th>
            <th>&nbsp;</th>
        </tr>
        <?php foreach ($incidents as $incident) :
        $ts = strtotime($incident->getDate_Opened());
        $date_opened_formatted = date('n/j/Y', $ts);
    ?>
    <tr>
        <td><?php echo htmlspecialchars($customer->getFullName()); ?></td>
        <td><?php echo htmlspecialchars($incident->getProduct_Code()); ?></td>
        <td><?php echo htmlspecialchars($incident->getTitle()); ?></td>
        <td><?php echo htmlspecialchars($incident->getDescription()); ?></td>
        <td><?php echo $date_opened_formatted; ?></td>
        <td><form action="." method="post">
            <input type="hidden" name="action"
                   value="select_incident">
            <input type="hidden" name="product_code"
                   value="<?php echo htmlspecialchars($incident->getIncident_id()); ?>">
            <input type="submit" value="Select">
        </form></td>
    </tr>
    <?php endforeach; ?>
    </table>

//****may have been looking at too long!

1 个答案:

答案 0 :(得分:0)

您应该为代码添加一些错误处理。数据库可以告诉您事件未定义。从事故中选择数据,但事件用于识别字段。

我的意思是尝试更改您的陈述以符合您的情况

 $query = 'SELECT concat(customer.firstName,customer.lastName) AS name,
    inc.productCode, inc.dateOpened, inc.title,
    inc.description FROM incidents inc

    INNER JOIN customers
    ON inc.customerID = customer.customerID
    WHERE techID IS NULL';

您的数据库能够为您提供错误消息。这些错误消息描述了您的陈述的问题