smartweb 发表于 2012-2-9 11:11:39

CI随机取出记录

我这里有普通的PHP和MYSQL代码,想转成CI Model的。

$db_con=mysql_connect('localhost', "root", "123456789101111111111111111111");
mysql_select_db( "sasoucing",$db_con);
$sql = "SELECT * FROM cms_products ORDER BY rand() LIMIT 5;";
$result=mysql_query($sql);




// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
      echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($db_con);

Hex 发表于 2012-2-9 11:17:59

参考一下 http://codeigniter.org.cn/user_guide/database/active_record.html

smartweb 发表于 2012-2-9 12:18:46

$sql = "SELECT * FROM cms_products ORDER BY rand() LIMIT 5;";
$query=$this->db->query($sql);
页: [1]
查看完整版本: CI随机取出记录