简体   繁体   English

从Mysql数据库PHP MVC检索数据

[英]Retrieve data from Mysql database PHP MVC

Here is my code in model.php 这是我在model.php中的代码

public function getQuestion($id){

$query = mysql_query("select * from bm_question where que_catid = '".$id."'");
return $query;
}

public function getAnswerforQuestion($qid){

$query1 = mysql_query("select * from bm_answer where ans_quid = '".$qid."'"); 
return $query1;

}  

And also below is my controller.php 下面也是我的controller.php

public function index() {

$this->load->model('quizmodel');
$qArr = $this->bm_question->getQustions(que_id);

foreach ($qArr as $value) {    

//Output Question
echo $value ->que_question;

    $this->load->model('quizmodel');
$ansArr=$this->answer->getAnswerforQuestion($value ->que_id);
foreach ($ansArr as $value2) {    
    //output answer
    echo $value2 ->answer;
}
}

Finally I bellow error for this code. 最后,我在此代码中出现错误。

在此处输入图片说明

I have not tested but it should work.Try like below: Your Model: 我尚未测试,但应该可以。请按以下方式尝试:您的模型:

public function getQuestion($id){
   $query = $this->db->query("select * from bm_question where que_catid='$id'");
   return $query->result();
}

Load your model in your controller like below: 将模型加载到控制器中,如下所示:

function __construct()
{
    parent::__construct();
    $this->load->model('quizmodel');
}   

Your controller : 您的控制器:

public function index(){
$qArr = $this->bm_question->getQuestions($que_id);
foreach($qArr as $value){
    // your code    
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM