简体   繁体   English

从两个表中检索信息

[英]retrieving information from two tables

I have a table called incidents , that i'd like to retrieve 4 fields/columns from, but i need firstname and lastname from the customer table. 我有一个名为incidents的表,我想从中检索4个字段/列,但我需要customer表中的firstnamelastname I keep getting the following error: 我一直收到以下错误:

Warning: Invalid argument supplied for foreach() 警告:为foreach()提供了无效的参数

This is the relevant code: 这是相关代码:

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

******but this is the involved table ******但这是涉及的表

<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!

You should add some error handling to your code. 您应该在代码中添加一些错误处理。 The db could tell you that incident is not defined. 数据库可以告诉您事件未定义。 The data is selected from incidents but incident is used to identify the fields. 从事件中选择数据,但是事件用于标识字段。

I mean try changing your statement to match your circumstances 我的意思是尝试更改您的陈述以适应您的情况

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

Your database is able to give you error messages. 您的数据库能够给您错误消息。 These error messages describe what is your problem with your statement 这些错误消息描述了您的语句有什么问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM