php 使用 CodeIgniter 从 mysql 数据库中随机记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1627934/
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
Random record from mysql database with CodeIgniter
提问by designer-trying-coding
I researched over the internet, but could not find anything...
我在互联网上进行了研究,但找不到任何东西......
I have a mysql db, and records at a table, and I need to get random record from this table at every page load. how can I do that? Is there any func for that?
我有一个 mysql 数据库,并在一个表中记录,我需要在每个页面加载时从该表中获取随机记录。我怎样才能做到这一点?有什么功能吗?
Appreciate! thanks
欣赏!谢谢
SORTED: link: http://www.derekallard.com/blog/post/ordering-database-results-by-random-in-codeigniter/
排序:链接:http: //www.derekallard.com/blog/post/ordering-database-results-by-random-in-codeigniter/
$this->db->select('name');
$query = $this->db->get('table');
$shuffled_query = $query->result_array();
shuffle ($shuffled_query);
foreach ($shuffled_query as $row) {
echo $row['name'] . '<br />';
}
回答by Patrick O'Doherty
Codeigniter provides the ability to order your results by 'RANDOM' when you run a query. For instance
Codeigniter 提供了在您运行查询时按“随机”对结果进行排序的功能。例如
function get_random_page()
{
$this->db->order_by('id', 'RANDOM');
or
$this->db->order_by('rand()');
$this->db->limit(1);
$query = $this->db->get('pages');
return $query->result_array();
}
I've used this before and found it to work fine. Hope that helps
我以前用过这个,发现它工作正常。希望有帮助
回答by ty812
I don't know about codeigniter, but getting a random dataset is
我不知道 codeigniter,但获取随机数据集是
SELECT * FROM table ORDER BY RAND() LIMIT 1
The relevant part is "ORDER BY RAND()", obviously.
相关部分ORDER BY RAND()显然是“ ”。
回答by FrustratedWithFormsDesigner
Do you know how many records there are in the table? You could do something like this:
你知道表中有多少条记录吗?你可以这样做:
$count=mysql_exec('select count(*)-1 from some_table');
$count=rand(1,$count);
then:
然后:
select * from
some_Table
limit $count,1
回答by Dipanwita Das
This code snippet worked well for me.
这段代码片段对我来说效果很好。
$this->db->select('name');
$this->db->order_by('rand()');
$this->db->limit(1);
$query = $this->db->get('<table>'); //<table> is the db table name
return $query->result_array();
回答by vchakoshy
Getting random record from large table is very expensive.
Don't useORDER BY RAND().
从大表中获取随机记录非常昂贵。
不要使用ORDER BY RAND().
This is a bad idea, but if you have a small table no problem. In a huge databases this type of queries very slow.
这是一个坏主意,但如果你有一张小桌子也没问题。在庞大的数据库中,这种类型的查询非常慢。
回答by chantheoun
I use codeigniter with datamapper. This is the code which I use to get a record randomly from table Advertiser:
我将 codeigniter 与 datamapper 一起使用。这是我用来从表中随机获取记录的代码Advertiser:
$ad = new Advertiser();
$ad->limit(3);
$ad->order_by('id', 'RANDOM');
$ad->get();
回答by Sugan Krishna
SELECT product_id, title, description
FROM products
WHERE active = 1
AND stock > 0
ORDER BY RAND()
LIMIT 4
The ORDER BY RAND() clause returns random records! You can limit records also using LIMIT.
ORDER BY RAND() 子句返回随机记录!您也可以使用 LIMIT 限制记录。
回答by SNabi
Lets think we have table where we deleted some rows. There is maybe ID not continues correctly. For sample id: 1,5,24,28,29,30,31,32,33 (9 rows)
让我们认为我们有删除一些行的表。可能是 ID 没有正确继续。样品编号:1、5、24、28、29、30、31、32、33(9行)
mysql_num_rows returns 9
mysql_num_rows 返回 9
Another methods will return not existing rows: $count=9; //because mysql_num_rows()==9 $count=rand(1,$count); // returns 4 for sample, but we havn't row with id=4
另一种方法将返回不存在的行:$count=9; //因为mysql_num_rows()==9 $count=rand(1,$count); // 为样本返回 4,但我们没有 id=4 的行
But with my method you always get existing rows. You can separate code and use first 2 code anywhere on site.
但是使用我的方法,您总是可以获得现有的行。您可以分离代码并在站点的任何位置使用前 2 个代码。
// Inside of Controller Class
function _getReal($id,$name_of_table)
{
$Q=$this->db->where('id',$id)->get($name_of_table);
if($Q->num_rows()>0){return $Q;}else{return FALSE;}
}
function _getLastRecord($name_of_table)
{
$Q=$this->db->select("id")->order_by('id DESC')->limit("1")->get($name_of_table)->row_array();
return $Q['id'];
}
function getrandom()
{
$name_of_table="news";
$id=rand(1,$this->_getLastRecord($name_of_table));
if($this->_getReal($id,$name_of_table)!==FALSE)
{
echo $id;
// Here goes your code
}
else
{
$this->getrandom();
}
// END
回答by RajSar
Getting random record from large table is very expensive. But bellow this code is very effective ..
从大表中获取随机记录非常昂贵。但是下面这段代码非常有效..
$count=mysql_num_rows(mysql_query("select * from table_name WHERE SOME_OF_YOUR_CONDITION"));
$nums=rand(1,$count);
mysql_query(" select * from table_name WHERE SOME_OF_YOUR_CONDITION LIMIT $count,1");
This will be helpful ...
这将是有帮助的...
回答by Ahmed Mahmoud
This function retrieve all rows in table in random order
此函数以随机顺序检索表中的所有行
public function get_questions(){
$this->db->select('*');
$this->db->order_by('rand()');
$this->db->from('multiple_choices');
$query = $this->db->get();
return $query->result_array();
}

