-1

I am using this to get results from a database, but it limits to 1 record.

I don't want any limit; I tried removing it but that doesn't work .

$this->db->from('links');
$this->db->where('uniq', $nnn[0]->uniq);
$this->db->limit(1);
$q = $this->db->get(); 
$nnn[0]->linkos=$q->result()[0];
return $nnn;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Asokh Roy
  • 1
  • 4
  • 3
    what happens if you remove this line `$this->db->limit(1);` ? – shyammakwana.me Nov 08 '17 at 22:27
  • dont work , i mean that dont effect anything , – Asokh Roy Nov 08 '17 at 22:28
  • 1
    try printing sql query, and check what's final query being generated. https://stackoverflow.com/questions/6142099/how-to-print-sql-statement-in-codeigniter-model – shyammakwana.me Nov 08 '17 at 22:30
  • 1
    How many results are in the database where the column `uniq` is equal to whatever the value of `$nnn[0]->uniq` is? – Goose Nov 08 '17 at 22:43
  • remove: $this->db->limit(1); update: from: $nnn[0]->linkos=$q->result()[0]; to: $nnn[0]->linkos=$q->result(); – Mark Nov 08 '17 at 22:49
  • everytime when create new link it generates new Uniq, now i have like 35 , but everytime when generate it creates new. – Asokh Roy Nov 08 '17 at 22:49
  • @Mark i tried , now it not even get data of the first one . – Asokh Roy Nov 08 '17 at 22:52
  • @AsokhRoy you have to loop it, because it is now iterable. use foreach to loop it such as foreach($nnn[0]->linkos as $foo) { ... } in this loop you can get your data in $foo variable – Mark Nov 08 '17 at 23:06

1 Answers1

1
  • The $q->result() method in CodeIgniter returns an object (try using $q->result_array() for returning your resultset as an array)
  • Appending a [0] to the $q->result() method would seem to me like you're limiting your resultset (be it object or array), to the first item only.

Thus, leaving out the [0] part seems like a first step I would try in your case:

$nnn[0]->linkos=$q->result();

Sidestep:

  • Consider using $this->db->query("SELECT * FROM table WHERE...") to retrieve your resultset. It gives you more control over what actual query is executed, to get your rows. It also takes some pain out of debugging.
  • Additionally, explicitly putting the SQL statement in your code, also benefits:
    • code portability
    • colleagues or other developers who might have to take over some day, and who do not share the same level of experience with CodeIgniter that you do
RvT
  • 53
  • 1
  • 6
  • leaving out the [0] - not worked, before it gets data of only 1 , after removing o it dont get any data , and i am using a complex system if i change nything need to change in all , so if you can give me any fix for this code ony , without changing much would be great – Asokh Roy Nov 08 '17 at 22:55
  • Could you please describe your use case a little more? What is the starting situation? Why do you have to query the database, what are you trying to achieve with the results? Also some DDL for the tables would be nice. – RvT Nov 08 '17 at 22:59
  • the code i am using giving me only results of 1 row , other rows i cant get. now main thing is via uniue it connect with 2 different things, uniq works as a relation between to data, ( mean uniq is same in both ) . – Asokh Roy Nov 08 '17 at 23:07
  • I think that the limitation will be set in the WHERE clause, which is set in the `$this->db->where('uniq', $nnn[0]->uniq);` statement. Perhaps you could first try checking the value for the `uniq` property for the first object in the `$nnn`-array. See if it makes any sense. If it does, try running an explicit SQL statement, separately from CodeIgniter; perhaps you've got a MySQL prompt available. Thinking about it, it seems that the second line (containing the WHERE clause) should limit the resultset to just one row (intended behaviour). The third line seems like an extra safety measure. – RvT Nov 08 '17 at 23:22
  • @RvT - You are wrong to say `result()` returns an object. It returns an array - an array of objects. `result_array()` returns and array of arrays. – DFriend Nov 09 '17 at 02:44
  • @DFriend thanks for pointing that out! My bad. Would you then agree on, when using `$q->result[0]`, that would come down to the same thing as using `$q->row`? In other words, will they both return a single object? – RvT Nov 09 '17 at 22:06
  • Yes, I agree `$q->result()[0]` and `$q->row()` return the exact same thing. `row()` is a lot easier to read and understand though. – DFriend Nov 10 '17 at 14:01