php 在 CodeIgniter 中显示数据库表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15058463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:28:10  来源:igfitidea点击:

Display database table in CodeIgniter

phpmysqlcodeigniter

提问by user1733081

I am trying to display a table using CodeIgniter. I made a function to select all data from one table and display it using a foreach loop when the button is clicked. I am getting this error:

我正在尝试使用CodeIgniter显示表格。我做了一个函数来从一个表中选择所有数据,并在单击按钮时使用 foreach 循环显示它。我收到此错误:

Fatal error: Call to undefined method CI_DB_mysql_driver::result() in C:\Xampp\htdocs\Auction\application\models\bidding_model.php on line 47

This is my controller page:

这是我的控制器页面:

public function viewauction()
{
    $this->load->model('bidding_model');
    $data['query'] = $this->bidding_model->viewauction();   
    $this->load->view('auction_view', $data);
}

This is the model:

这是模型:

function viewauction()
{
    $query =  $this->db->select('products'); 
    return $query->result();
}

This is the view:

这是视图:

<tbody>
<?php foreach($query as $row): ?>
<tr>   
    <td><?php echo $row->product_id; ?></td>
    <td><?php echo $row->auction_id; ?></td>
    <td><?php echo $row->start_time; ?></td>
    <td><?php echo $row->end_time; ?></td>
</tr>
<?php endforeach; ?>
</tbody>

采纳答案by Madan Sapkota

Just change your model method code to

只需将您的模型方法代码更改为

function viewauction()
{
    $query = $this->db->select('*')->from('products')->get();
    return $query->result();
}

Hope this helps. Thanks!!

希望这可以帮助。谢谢!!

回答by Steward Godwin Jornsen

Your problem is here:

你的问题在这里:

$query =  $this->db->select('products'); 
return $query->result() ;

$query->result()is returning false probably because the products table does not exist. you have to use get instead of select.

$query->result()返回 false 可能是因为 products 表不存在。你必须使用 get 而不是 select。

Try:

尝试:

$query =  $this->db->get('products'); 
return $query->result() ;

That could get your started

这可以让你开始

回答by sonu sharma

public function select($table, $field, $value)
{
    $this->db->select(*);
    $this->db->from('$table');
    $this->db->where($field, $value);
    $query = $this->db->get();

    return $query;
}

I hope the above code will help you.

我希望上面的代码对你有帮助。

回答by Viral

There is actually a simpler way available.

实际上有一种更简单的方法可用。

You should get most from the framework features it is providing,

您应该从它提供的框架功能中获得最大收益,

Use, CodeIgniter's Table Library,

使用,CodeIgniter 的表库,

$this->load->library('table'); // Loading the Table Library 

$query = $this->db->get('table_name'); // the MySQL table name to generate HTML table

echo $this->table->generate($query); // Render of your HTML table

You can also modify the behavior of HTML generator if you want some custom things like class in table head or body or anything, which you will almost need.

如果你想要一些自定义的东西,比如表头或正文中的类或任何你几乎需要的东西,你也可以修改 HTML 生成器的行为。

$this->table->set_template($template); // passing an array

Use this line after loading the table library. Use keys from the documentation link below.

加载表库后使用此行。使用以下文档链接中的密钥。

Reference: CodeIgniter 3 Table Library - Official Docs

参考:CodeIgniter 3 表库 - 官方文档

回答by Rakesh Kumar Sharma

function viewauction()
{
    $this->db->select('*'); 
    $this->db->from('tablename');
    $query = $this->db->get();
    return $query->result();
}

Above code will help you.

上面的代码会帮助你。

回答by tomexsans

you have to use get()

你必须使用 get()

select()query builder is used for selecting the columns of the table and not the table

select()查询生成器用于选择表的列而不是表

example

例子

$query = $this->db->select(array('product_id','auction_id'))
                  ->get('products');
return $query->result();

if you want to select all you can use the get only,

如果你想选择所有你可以使用的 get only,

read more at http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

http://ellislab.com/codeigniter/user-guide/database/active_record.html#select阅读更多