php CodeIgniter - mysql while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7521508/
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
CodeIgniter - mysql while loop
提问by ryryan
I am relatively experienced PHP developer.
我是比较有经验的 PHP 开发人员。
I have chosen the CodeIgniter framework. So far I have understood how it is used. Until now, I want to be able to do a while loop.
我选择了 CodeIgniter 框架。到目前为止,我已经了解它是如何使用的。到现在为止,我希望能够做一个 while 循环。
Within my controller I have:
在我的控制器中,我有:
$this->load->model('Test');
$data['result'] = $this->Test->getInfo();
$this->load->view('index1', $data);
In my Model I have:
在我的模型中,我有:
$result = mysql_query("SELECT * FROM users WHERE last_name='Hart'")or die(mysql_error());
$row = mysql_fetch_array( $result );
return $row;
And in my View:
在我看来:
echo $result[0];
However, this only outputs the first field within the first row found.
但是,这仅输出找到的第一行中的第一个字段。
Please could you help me with retrieving the information from a database - while loop.
请您帮我从数据库中检索信息 - while 循环。
Does the while loop happen in the Model? Am I just echoing out the result correctly?
while 循环是否发生在模型中?我只是正确地回显结果吗?
回答by Ben
There's actually a much easier way to do what you want to do using the ActiveRecord class that CodeIgniter includes which will allow you to just return an array of results. Here's the documentation.
实际上有一种更简单的方法可以使用 CodeIgniter 包含的 ActiveRecord 类来做你想做的事情,它允许你只返回一个结果数组。这是文档。
Your model would become:
你的模型会变成:
$this->db->where('last_name','Hart');
$result = $this->db->get('users');
return $result->result_array();
You may also have to setup your database in application/config/database.php
, and also load the database class in application/config/autoload.php
:
您可能还需要在 中设置数据库application/config/database.php
,并在 中加载数据库类application/config/autoload.php
:
$autoload['libraries'] = array('database');
To display this information, using the MVC pattern properly your controller should pass the information it gets from the model to a view. To do this generally you do this:
要显示此信息,请正确使用 MVC 模式,您的控制器应该将从模型获取的信息传递给视图。要做到这一点,通常你这样做:
$data['myinfo'] = $this->test->getInfo();
$this->load->view('test_view',$data);
Then you have to create a view like so in applications/views/test_view.php
:
然后你必须像这样创建一个视图applications/views/test_view.php
:
<h1>Some HTML goes here</h1>
<?php
foreach($myinfo as $row) {
echo $row['field'];
}
?>
<p>Some more HTML goes here</p>
I suggest you read the CodeIgniter User Guidebefore diving into creating an application as CodeIgniter includes libraries that greatly simplify and speed up the whole process.
我建议您在开始创建应用程序之前阅读CodeIgniter 用户指南,因为 CodeIgniter 包含的库可以极大地简化和加快整个过程。
回答by user834418
Well mysql_fetch_array will only bring back one row at a time, so you would need to put the while loop around the mysql_fetch_array call
那么 mysql_fetch_array 一次只会带回一行,所以你需要在 mysql_fetch_array 调用周围放置 while 循环
Something like
就像是
while($row = mysql_fetch_array($result)) {
//save/process data
}
while($row = mysql_fetch_array($result)) {
//save/process data
}
I would however just use codeigniters libraries to load the db and then use it:
然而,我只会使用 codeigniters 库来加载数据库,然后使用它:
$this->load->database();
$result = $this->db->query("SELECT * FROM users WHERE last_name='Hart'");
$result = $result->result_array();
//check for data and handle errors
return $result[0]
$this->load->database();
$result = $this->db->query("SELECT * FROM users WHERE last_name='Hart'");
$result = $result->result_array();
//check for data and handle errors
return $result[0]