Welcome Guest, Not a member yet? Register   Sign In
I can't get a simple id
#1

[eluser]Sein Kraft[/eluser]
I cant get the last added id (is an autoincrement id in sql) to use in other table.
Model:
Code:
function id()
    {
        $query = $this->db
        ->select('id')
        ->from('imageboard_image')
        ->limit(1)
        ->order_by('id','desc');
        
        return $query;
    }

Error:
Quote:A PHP Error was encountered

Severity: 4096

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

Filename: libraries/Parser.php

Line Number: 63
#2

[eluser]Pascal Kriete[/eluser]
Judging by the error, and seeing as it's thrown in the parser, I think the query works fine.

You're not grabbing the result. I think what you're looking for is:
Code:
return $query->row();

Check out the query result documentation for more info.
#3

[eluser]Sein Kraft[/eluser]
Yes, I've tried grab the result using row but this throw the next error

Quote:Fatal error: Call to undefined method CI_DB_mysql_driver::row() in E:\server\htdocs\libelula\system\application\models\model_imageboard.php on line 48
#4

[eluser]Sein Kraft[/eluser]
Yes, I've tried grab the result using row but this throw the next error

Quote:Fatal error: Call to undefined method CI_DB_mysql_driver::row() in E:\server\htdocs\libelula\system\application\models\model_imageboard.php on line 48
#5

[eluser]Pascal Kriete[/eluser]
Ok, it might be the query after all then.
Code:
return ($query->num_rows() > 0) ? $query->row() : FALSE;

If that returns false the query isn't returning anything. Also, I'm curious in what situation you only need the id?
#6

[eluser]Sein Kraft[/eluser]
It doesn't work :S

Quote:Fatal error: Call to undefined method CI_DB_my::num_rows() in E:\server\htdocs\libelula\system\application\models\model_imageboard.php on line 48

I need the id of the latest record to insert in another table.

When I upload a image i assign an id (auto-increment in sql), then i need retrieve that id and insert in other table where the id is inserted with a tag system.

For Example:
I have a form with two inputs, one for the file and other for the tags who represent the image.

For the first instance I insert in the table (images) the data of the uploaded image.
Next I need retrieve the id of the uploaded image from the table (images) and get the tags from the second input of the form and save in the table (tags).

Code:
function insert($data)
    {
        
        //INSERT IMAGE IN IMAGES
        $user_id = '1';
        $user_ip = '127.0.0.1';
        $file_hash = 'myhash';
        
        $file_name = $data['file_name'];
        $file_ext = $data['file_ext'];
        $file_size = $data['file_size'];
        $file_width = $data['file_width'];
        $file_height = $data['file_height'];
        $file_source = $data['file_source'];
        $file_tag = $data['file_tag'];
        
        $data = array(
               'id' => 'NULL' ,
               'user_id' => $user_id,
               'user_ip' => $user_ip,
               'file_name' => $file_name,
               'file_hash' => $file_hash,
               'file_ext' => $file_ext,
               'file_size' => $file_size,
               'file_height' => $file_height,
               'file_width' => $file_width,
               'file_source' => $file_source
            );

        $this->db->insert('images', $data);
                      
        //INSERT TAGS

        $query = $this->db
        ->select('id')
        ->from('images')
        ->limit(1)
        ->order_by('id','desc');
                
        $data = array(
               'image_id' => $query ,
               'image_tag' => $file_tag
            );

        $this->db->insert('tags', $data);

    }

Function id() is a test function because I can't do this work.
#7

[eluser]Pascal Kriete[/eluser]
Right now you're not querying for the id at all, you need to add a ->get(). However, I had a hunch you were trying to do this.
There's a helper function that you might find helpful Wink :
Code:
$this->db->insert_id();
#8

[eluser]Sein Kraft[/eluser]
[quote author="inparo" date="1221337876"]Right now you're not querying for the id at all, you need to add a ->get(). However, I had a hunch you were trying to do this.
There's a helper function that you might find helpful Wink :
Code:
$this->db->insert_id();
[/quote]

And from where gets the id the function?
#9

[eluser]Pascal Kriete[/eluser]
It has access to the connection identifier, so in the case of mysql it'll use mysql_insert_id.
#10

[eluser]Sein Kraft[/eluser]
Oh, I see.

Thanks for you help =)




Theme © iAndrew 2016 - Forum software by © MyBB