我有一个名为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!

最佳答案

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

我的意思是尝试更改您的陈述以适应您的情况

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


您的数据库能够给您错误消息。这些错误消息描述了您的语句有什么问题

关于php - 从两个表中检索信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35531366/

10-10 02:45