Php 从查询(不工作)未定义变量获取特定字段:fetch\ U data

Php 从查询(不工作)未定义变量获取特定字段:fetch\ U data,php,html,codeigniter,Php,Html,Codeigniter,我试图从查询中获取不同的列 我在控制器文件中使用此功能: public function fetch_data(){ $result = $this->db->query("select * from table1 oa inner join table2 kio on oa.id = kio.id_table_1 inner join table3 joi on kio.col3 = joi.col3_id_table_2 w

我试图从查询中获取不同的列

我在控制器文件中使用此功能:

                public function fetch_data(){
            $result = $this->db->query("select * from table1 oa
inner join table2 kio on oa.id = kio.id_table_1
inner join table3 joi on kio.col3 = joi.col3_id_table_2
where joi.processed = 1
and joi.approved = 1
and not exists (select 1 
                from table3 t3 
                where t3.processed = 0 
                      and t3.approved = 0 
                      and t3.col3_id_table_2 = joi.col3_id_table_2)");
            return $result;
                }
这是MySQL语句:

我在此视图中调用该函数:

<?php include('header.php'); ?>

<html>
<head>

</head>
<body>

    <div class="container"> 
    <div class="row">
    <div class="col-md-12">

        <h2 align="center">TABLE:USERS</h2>

                <thead>
                    <th>id</th>
                    <th>COL2</th>
                    <th>ID_TABLE_1</th>
                    <th>COL2</th>
                    <th>COL3</th>
                    <th>COL3_ID_TABLE2</th>
                    <th>PROCESSED</th>
                    <th>APPROVED</th>
                </thead>


<tbody>

<?php

    if ($fetch_data->num_rows() > 0)
     { 

        foreach($fetch_data->result() as $record) {
        echo "<tr> 
    <td>".$record['id']."</td> 
    <td>".$record['COL2']."</td> 
    <td>".$record['ID_TABLE_1']."</td> 
    <td>".$record['COL2']."</td> 
    <td>".$record['COL3']."</td> 
    <td>".$record['COL3_ID_TABLE2']."</td> 
    <td>".$record['PROCESSED']."</td> 
    <td>".$record['APPROVED']."</td> 

    </tr>"; 

    }
    }


?>

</tbody>


    </table>

        </div>
        </div>
        </div>

</body>
</html>

表:用户
身份证件
可乐
ID_表_1
可乐
可乐
COL3\u ID\u表2
处理
经核准的
但是,我得到了这个错误消息:未定义变量:fetch_data

我做错了什么

编辑:

我尝试在视图中运行此代码:

<?php
$this->fetch_data();
$myVar = fetch_data();
if ( $myVar->num_rows() > 0 ) {
    foreach ( $myVar->result() as $record ) {
        echo "<tr>"
             . "<td>" . $record['id'] . "</td>"
             . "<td>" . $record['COL2'] . "</td>"
             . "<td>" . $record['ID_TABLE_1'] . "</td>"
             . "<td>" . $record['COL2'] . "</td>"
             . "<td>" . $record['COL3'] . "</td>"
             . "<td>" . $record['COL3_ID_TABLE2'] . "</td>"
             . "<td>" . $record['PROCESSED'] . "</td>"
             . "<td>" . $record['APPROVED'] . "</td>"
             . "</tr>";
    }
}

您需要将
fetch_data
函数的返回赋值给一个变量,以便访问它

$myVar = fetch_data();
if ( $myVar->num_rows() > 0 ) {
    foreach ( $myVar->result() as $record ) {
        echo "<tr>"
             . "<td>" . $record['id'] . "</td>"
             . "<td>" . $record['COL2'] . "</td>"
             . "<td>" . $record['ID_TABLE_1'] . "</td>"
             . "<td>" . $record['COL2'] . "</td>"
             . "<td>" . $record['COL3'] . "</td>"
             . "<td>" . $record['COL3_ID_TABLE2'] . "</td>"
             . "<td>" . $record['PROCESSED'] . "</td>"
             . "<td>" . $record['APPROVED'] . "</td>"
             . "</tr>";
    }
}

$myVar=fetch_data();
如果($myVar->num\u rows()>0){
foreach($myVar->result()作为$record){
回声“”
.“.$record['id']”
.“.$record['COL2']”
.“.$record['ID\u TABLE\u 1']”
.“.$record['COL2']”
.“.$record['COL3']”
.“.$record['COL3\u ID\u TABLE2']”
.“.$record['PROCESSED']”
.“.$record['APPROVED']”
. "";
}
}

fetch_data
是一个函数,而不是变量,因此不能这样处理。调用函数并将结果分配给变量。您好@aynber谢谢您的回复!。我试着这么做,但出于某种原因,它对我不起作用。你将如何修改它?fetch_data()->num_rows()fetch_data()->result()@abracadver嗨,伙计,我试图这么做,但我得到了这个错误:Type:parserro消息:语法错误,意外的'->'(T_OBJECT_操作符)Hi@Danny是的,我试图这么做,但我得到了这个错误:Type:parserror消息:语法错误,意外的'->'(T_OBJECT_操作符)嘿,老兄,谢谢你的回复!。我收到了以下错误消息:Type:error message:Call to undefined function fetch_data()@Student_new您应该将数据放入一个变量并将其传递到视图中,然后循环遍历该变量。不确定您正在使用的框架,但您可以尝试
$this->fetch_data()
调用控制器函数。否则,您将必须了解如何访问控件以调用内容-编辑或做什么Dannysaid@AquilaSagitta我已经更新了我的问题,请回答look@Danny嗨,丹尼,我更新了我的问题