如何从cakephp中的表中获取数据

时间:2014-07-21 09:55:14

标签: mysql cakephp cakephp-2.0

您好我的数据库中有一个表有状态列表,我想从表中获取此数据,但我的查询没有正确执行它给了我一些错误

<?php
                require_once('../Config/database.php');
                $result1=$this->Signup->query("SELECT * FROM states");

 //echo $popular;
 while($post = mysql_fetch_array($result1))

{ ?>

                <table width="380">
                          <tr>
                            <td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $post['state_name']?>&state_id=<?php echo $post['state_id']?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $post['state_name']?></a></td>

                          </tr>
                    </table>
                    <?php   } 
?>

但它给了我错误 警告(512):方法SignupHelper :: query不存在[CORE \ Cake \ View \ Helper.php,第192行 警告(2):mysql_fetch_array()期望参数1为资源,给定

为null

1 个答案:

答案 0 :(得分:1)

请先阅读the documentation

您似乎正试图通过查询获取View中的状态。 您需要将视图与模型分开。

创建State模型。

在控制器中使用类似的内容:

$this->loadModel('State');
$states = $this->State->find('list'); // this will create a key => value array with the IDs and names
$this->set('states', $states);

在您看来,请使用

<table width="380">
 <tr>
    <?php foreach ($states as $stateId => $stateName) {
    <td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $stateName?>&state_id=<?php echo $stateId?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $stateName ?>></a></td>
    <?php } ?>
</tr>        

您可能仍需要进行一些更改,但这是主要想法。

相关问题