Take the 2-minute tour ×

Hi i have one table in my database which has list of states and i want to fetch this data from the table but my query is not executing properly it gives me some error

                $result1=$this->Signup->query("SELECT * FROM states");

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

{ ?>

                <table width="380">
                            <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>

                    <?php   } 

But it gives me error Warning (512): Method SignupHelper::query does not exist [CORE\Cake\View\Helper.php, line 192 Warning (2): mysql_fetch_array() expects parameter 1 to be resource, null given

share|improve this question
You obviously haven’t bothered to read the CakePHP documentation, otherwise you wouldn’t be running raw SQL queries, in a view. If you can’t be bothered to read documentation, weren’t not going to be bothered to answer your questions. –  Martin Bean Jul 21 at 11:08

1 Answer 1

up vote 1 down vote accepted

Please read the documentation first.

It seems you are trying to get the states, inside the View, with a query. You need to separate the view from the model.

Create a State model.

Use something like this in your controller:

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

In your view, use

<table width="380">
    <?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 } ?>

You might still need some changes, but this is the main idea.

share|improve this answer
i have created the State model and i have used your code in controller and in virew page but it gives me this error Call to a member function find() on a non-object C:\wamp\www\test\app\Controller\SignupsController.php –  Jha Jul 21 at 10:59
I added $this->loadModel('State'); in the answer and changed to $this->State->find... –  cornelb Jul 21 at 11:07
it's worked perfectly but i have one doubt that if i need to fetch the data from 5 table then i need to create the 5model means individual model for every table or there is any other way to do this –  Jha Jul 21 at 11:32
Each table should have its own model. This is how you should work. Later on, when you add more functions related to one of the tables/models, it will be easier –  cornelb Jul 21 at 11:55
Can you accept the answer if the suggestion worked? –  cornelb Jul 21 at 12:00

Your Answer


