EllisLab text mark
Advanced Search
     
Mysql get, where, order by and limit in one.
Posted: 27 June 2009 07:24 PM
Joined: 2009-06-27
34 posts

Hello Everyone,

I am working on a new application, this is the first week I use CI so this is my first (and hope last) problem.

When I use the code like:

controllers/artists.php

$songsQuery $this->db->get('songs'); // Songs query
$songsQuery $this->db->where('artist'$getMysql->id); // Where
$songsQuery $this->db->order_by("hits"); // Order by
$songsQuery $this->db->limit(15); // Limit, 15 entries 

views/blocks/songs

<h1><?$Heading?></h1>

<
ul>
    
<? foreach ( $songs->result_array() as $Row ) : ?>
    
    
<li><?anchor('videos-lyrics/view/'.$Row->id.'/'.$artistRow->name.'/'.$Row->title'My News'); ?> <small><?$Row->hits;?> x bekeken</small></li>
    
    
<? endforeach; ?>
</ul

 
I get this error:

Fatal error: Call to undefined method CI_DB_mysql_driver::result_array() in ****/songscript/application/views/blocks/songs.php on line 4

I’ve searching with Google but I couldn’t find the solution.

Can anybody help me?

Thanks a lot!

Kind Regards,
Ronny0

Edit: I see I’m in the wrong category/forum, sorry for that.

 
Posted: 27 June 2009 07:37 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

Hi R. Oerlemans,

Firstly, I think you should not fetch your data in views; do it in your models and return the result as an array, and then assign the array to your views. smile

 
Posted: 27 June 2009 07:43 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-06-27
34 posts
BnoL - 27 June 2009 11:37 PM

Hi R. Oerlemans,

Firstly, I think you should not fetch your data in views; do it in your models and return the result as an array, and then assign the array to your views. smile

Thanks for the tip, i’ve solved that.

 

/* Data for song block */            
    
$songsQuery $this->db->get('songs'); // Songs query
    
$songsQuery $this->db->where('artist'$getMysql->id); // Where
    
$songsQuery $this->db->order_by('hits'); // Order by
    
$songsQuery $this->db->limit(15); // Limit, 15 entries
    
$songsQuery $this->db->result_array(); // Fetch All 

Fatal error: Call to undefined method CI_DB_mysql_driver::result_array() in *****/application/controllers/artists.php on line 50

 
Posted: 27 June 2009 07:48 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

Have you loaded the database library?

 
Posted: 27 June 2009 07:52 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

I think your code should look like this:

$this->db->get('songs'); // Songs query
$this->db->where('artist'$getMysql->id); // Where
$this->db->order_by('hits'); // Order by
$this->db->limit(15); // Limit, 15 entries
$songsQuery $this->db->query(); 
$songsQuery->result_array(); 
 
Posted: 27 June 2009 07:58 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2009-06-27
34 posts
BnoL - 27 June 2009 11:52 PM

I think your code should look like this:

$this->db->get('songs'); // Songs query
$this->db->where('artist'$getMysql->id); // Where
$this->db->order_by('hits'); // Order by
$this->db->limit(15); // Limit, 15 entries
$songsQuery $this->db->query(); 
$songsQuery->result_array(); 

Thanks again, now I get an CI error in place of a fatal error.

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for CI_DB_driver::query(), called in /home/vision043/domains/wpcoder.nl/public_html/songscript/application/controllers/artists.php on line 50 and defined

Filename: database/DB_driver.php

Line Number: 244
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: sql

Filename: database/DB_driver.php

Line Number: 246
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: sql

Filename: database/DB_driver.php

Line Number: 250

De query die u hebt ingegeven is niet correct.

 
Posted: 27 June 2009 08:08 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

Try this,

$this->db->where('artist'$getMysql->id);
$this->db->order_by('hits');
$this->db->limit(15);
$songsQuery $this->db->get('songs');
$songsQuery->result_array(); 
 
Posted: 27 June 2009 08:12 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-06-27
34 posts

Thanks, this works! Thanks a lot!

 
Posted: 27 June 2009 08:16 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

No problem. I’m a newbie too raspberry. Just read the manual more carefully wink

 
Posted: 27 June 2009 08:20 PM   [ # 9 ]   [ Rating: 0 ]
Joined: 2009-06-27
34 posts

English isn’t my first language. So it’s a little bit hard to me.

 
Posted: 27 June 2009 08:24 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-05-29
19 posts

Me too, I’m Vietnamese. A dictionairy might be helpful for us raspberry

 
Posted: 12 May 2010 05:43 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-11
4 posts

Funny, english is my first language and while looking through the user guide I still had trouble trying to figure this one out! It is late and I am a little tired, but I did a lot of Googling and this topic finally popped up.

 Signature 

I’m feeling like a star, you can’t stop my shine.
I’m loving cloud nine, my head’s in the sky.
I’m solo, I’m riding solo.

 
Posted: 21 May 2010 06:37 AM   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-05-21
2 posts

Hellow, I am new, and I have a problem: my datas are displated outside, not in the website

code:
$data = array();
    $this->data[‘CONTENT’] = $this->load->view(‘Forms/FormCautareNum’, $data, TRUE);
    $this->parser->parse(‘layout/base_template’,$this->data);   
   
    if ($this->input->post(‘btnAfisNum’))
    {
    $var=$this->input->post(‘textfield’);
    $query = $this->db->query(“SELECT * FROM adaugare_firma WHERE Nume LIKE ‘%$var%’”)->result();
    foreach($query as $q)
    {
          echo q->Nume;
          echo q->Domeniu;
          echo q->Subdomeniu;
          echo q->produs;
        }

and I want to parse those datas in my view form: FormCautareNum .... Can anyone help?