leeloo 发表于 2012-3-3 10:01:02

[数据库]无法获数据

昨天写了段代码,想嵌套搜索数据库

数据库test共有两个表,一个叫做test,一个叫做relation
test表中有 id 和 letters 两列,get_words搜索test表
relation表中有 relation_id, mot_1 和 mot_2 三列,get_relation_mots搜索relation表

控制器的php中
$data['relation_mots'] = $this->words_model->get_relation_mots('AAA');//获得relation表中AAA的关联词条目,数据库中共有两个

$counter = 0;
foreach ($data['relation_mots'] as $relation_mots_item):
{
        $data['relation_mots_id'][$counter] = $this->words_model->get_words($relation_mots_item['mot_2']); //把两个关联词的在test表中的信息分别给$data['relation_mots_id'] 和$data['relation_mots_id']。
        $counter = $counter+1;
}
endforeach;

$data['counter'] = $counter; //传递计数器

视图php中
for ($relation_mots_id_counter=0; $relation_mots_id_counter<$counter; $relation_mots_id_counter++)
{
        echo $relation_mots_id2[$relation_mots_id_counter]['id']; //输出获取的test表中的id值
}


但是终端页面显示:
“A PHP Error was encountered
Severity: Notice
Message: Undefined index: id”
为什么会出现这样的情况啊?应当怎么修改呢?

谢谢了

yuzhigang5460 发表于 2012-3-3 10:11:21

谁知道你get_words里干啥了。最关键的代码没贴出来。

lynn.wang 发表于 2012-3-3 11:56:25

尽然是操作数据库
你肯定要吧 model 和 controller 的code 贴出来啦!

leeloo 发表于 2012-3-3 13:10:12

本帖最后由 leeloo 于 2012-3-3 13:28 编辑

明白了。

words_model.php
<?php
class Words_model extends CI_Model {

      public function __construct()
      {
                $this->load->database();      //载入数据库
      }

      public function get_words($zimu)      //查询词
      {
                $query = $this->db->query("SELECT * FROM test WHERE letters = '$zimu';");
                return $query->result_array();
      }
      
      public function get_relation_mots($zimu)      //查询关联词
      {
                $query = $this->db->query("SELECT mot_2 FROM relation WHERE mot_1 = '$zimu';");
                return $query->result_array();
      }
}

words_db.php
<?php
class Words_db extends CI_Controller {

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

      public function index()
      {
                $data['words'] = $this->words_model->get_words('AAA');
                $data['title'] = 'Words';
                $data['relation_mots'] = $this->words_model->get_relation_mots('AAA');
               
                $counter = 0;
                foreach ($data['relation_mots'] as $relation_mots_item):
                {
                        $data['relation_mots_id2'][$counter] = $this->words_model->get_words($relation_mots_item['mot_2']);
                        $counter = $counter+1;
                }
                endforeach;
               
                $data['counter'] = $counter;
               
                $this->load->view('templates/header', $data);
                $this->load->view('words_db/index', $data);
                $this->load->view('templates/footer');
      }
}

index.php
<?php foreach ($words as $words_item):      ?>

      <h2><?php echo $words_item['id'] ?>, <?php echo $words_item['letters'] ?></h2>
      <p><a href="words_db/<?php echo $words_item['letters'] ?>">View article</a></p>

<?php endforeach ?>

<?php foreach ($relation_mots as $relation_mots_item):?>

      <h2>Relation Mots: <?php echo $relation_mots_item['mot_2'] ?></h2>
      <p><a href="words_db/<?php echo $relation_mots_item['mot_2'] ?>">Relation Mots</a></p>

<?php endforeach ?>


<?php

for ($relation_mots_id_counter=0; $relation_mots_id_counter<$counter; $relation_mots_id_counter++)
{
      echo $relation_mots_id2[$relation_mots_id_counter]['id'];
}
?>

justdoit 发表于 2012-3-3 13:34:57

这种情况最常见的解决办法是你先把那个sql输出来,看看是不是sql写错了,或者把结果集打印出来看包不包含你要取的结果。{:1_1:}

leeloo 发表于 2012-3-3 14:36:14

收到,谢谢

<?php

for ($relation_mots_id_counter=0; $relation_mots_id_counter<$counter; $relation_mots_id_counter++)
{
      echo $relation_mots_id2[$relation_mots_id_counter]['id'];
}
?>

如果把$relation_mots_id2[$relation_mots_id_counter]['id']; 改成 $relation_mots_id2[$relation_mots_id_counter];的话
错误信息没有了,变成了“ArrayArray”。

如果把words_db.php中的$data['relation_mots_id2'][$counter] = $this->words_model->get_words($relation_mots_item['mot_2']); 取消掉[$counter]
同时在index.php中同时取消$counter,直接输出,如下
foreach ($relation_mots_id as $relation_mots_id_item):
      echo $relation_mots_id_item['id'];
endforeach;
是没有问题的,但是由于没有了计数,本来应该有两组数据的,结果两组数据被重复赋予$data['relation_mots_id'],最终只有最后一组数据被$data['relation_mots_id']传递,这样在最终页面显示的时候,就只剩下一个id输出了。

lynn.wang 发表于 2012-3-4 15:05:20

嘿嘿 !
页: [1]
查看完整版本: [数据库]无法获数据