PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] select max codeigniter

 March 05, 2022     codeigniter, mysql, php     No comments   

Issue

Im trying to get an max value with codeigniter from an table but it isnt working. This is the error i get:

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: database/DB_active_rec.php

Line Number: 427

This is my function:

public function getPeriodeNummer($bedrijf_id) {
    $this->db->select_max('id');
    $this->db->where('bedrijf_id', $bedrijf_id);
    $result = $this->db->get('rapporten');

    $this->db->select('periode_nummer');
    $this->db->where('rapporten_id', $result);
    $query = $this->db->get('statistieken_onderhoud');

    $data = $query + 1;

    return $data;
}

What im trying to do is as followed:

  1. Select the highest id where bedrijf_id = $bedrijf_id from rapporten.
  2. Select the periode_nummer from statistieken_onderhoud where rapporten_id = the highest id i got from step 1.
  3. Add 1 to the periode_nummer i got from step 2 and return that number.

Thanks in forward for your help!


Solution

Try

public function getPeriodeNummer($bedrijf_id) {
    $this->db->select_max('id');
    $this->db->where('bedrijf_id', $bedrijf_id);
    $res1 = $this->db->get('rapporten');

    if ($res1->num_rows() > 0)
    {
        $res2 = $res1->result_array();
        $result = $res2[0]['id'];

        $this->db->select('periode_nummer');
        $this->db->where('rapporten_id', $result);
        $query = $this->db->get('statistieken_onderhoud');

        if ($query->num_rows() > 0)
        {
            $row = $query->result_array();
            $data['query'] = 1 + $row[0]['periode_nummer'];
        }

        return $data['query'];
    }

    return NULL;
}


Answered By - stealthyninja
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing