php - 我希望我的代码从存储在数据库中的列中请求特定值
问题描述
class quizmodel extends CI_Model {
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3, answer, subject");
$this->db->from("quiz");
$this->db->where?????
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
这是我的代码,我想要它以便“主题”字段只要求“计算”。或者“quizID”要求从 1 到 10 的 quizID。
解决方案
如果你想获得quizID
1-10 范围,那么你可以使用如下范围:
$this->db->where('quizID >=', 1);
$this->db->where('quizID <=', 10);
或者,
$this->db->where("quizID BETWEEN 1 AND 10");
如果您只想获取computing
相关数据,则可以LIKE
在此处使用:
$this->db->like('subject', 'computing');
一些额外和有用的信息:
$this->db->like('subject', 'computing', 'before'); // Produces: WHERE `subject` LIKE '%computing'
$this->db->like('subject', 'computing', 'after'); // Produces: WHERE `subject` LIKE 'computing%'
$this->db->like('subject', 'computing', 'none'); // Produces: WHERE `subject` LIKE 'computing'
$this->db->like('subject', 'computing', 'both'); // Produces: WHERE `subject` LIKE '%computing%'
推荐阅读
- java - Eclipse sts IDE中有没有办法从文件中批量加载许多环境变量
- r - 当时间序列在行而不是列中时绘图 - 使用 R
- scala - Scala Logging: is LazyLogging blocking or non-blocking?
- java - Selenium driver.findElement 找到元素,sendKeys 工作,然后在移动到新字段时为空
- python - 用于清理数据集的 Pandas 布尔语句
- python - 如何压缩矩阵以避免内存错误?
- spring-boot - 带有 Spring Boot 的 CORS - 将 GET 请求限制到某些域
- java - 将 2 个 Java 变量简化为一个
- wordpress - 修改循环以排除属于某个类别且没有其他类别的帖子
- xpages - 如何计算重复控件中显示的行数