Sky_Sailor Posted May 19, 2014 Share Posted May 19, 2014 hey developers, i have problem to get last insert id. btw.. i try to put that code on my mvc creation. on register_model page i write like this: public function numbering(){ $sth = $this->db->prepare('SELECT * FROM member_data'); $result = $this->db->lastInsertId(); return $result; } and for the controllers page, i write the code like this: $c = $this->model->numbering(); foreach ($c as $key => $value) {$data = $result('id'); } $abc['member_id'] = $data; but i didn't get the results,.. can you help me? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/ Share on other sites More sharing options...
Jacques1 Posted May 19, 2014 Share Posted May 19, 2014 (edited) Hi, no offense, but this makes absolutely no sense whatsoever. You're asking for the ID of the last inserted row, but there's no INSERT query in your code. You prepare a SELECT query, but you never execute it. If the method worked, it would return a single number, but then you try to loop over this number as if it was some kind of collection. If the loop worked, it would just overwrite the same variable over and over again. What is $result('id') supposed to be? Pretty much every line is a mystery. Are you sure you've thought this through? Maybe what you actually want is the biggest member ID? That's one line of code: $last_member_id = $this->db->query('SELECT MAX(id) FROM member_data')->fetchColumn(); Edited May 19, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480025 Share on other sites More sharing options...
IanA Posted May 19, 2014 Share Posted May 19, 2014 If you're new to PDO, the best place to start is the PHP manual...http://www.php.net/manual/en/book.pdo.php Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480031 Share on other sites More sharing options...
Sky_Sailor Posted May 19, 2014 Author Share Posted May 19, 2014 Hi, no offense, but this makes absolutely no sense whatsoever. You're asking for the ID of the last inserted row, but there's no INSERT query in your code. You prepare a SELECT query, but you never execute it. If the method worked, it would return a single number, but then you try to loop over this number as if it was some kind of collection. If the loop worked, it would just overwrite the same variable over and over again. What is $result('id') supposed to be? Pretty much every line is a mystery. Are you sure you've thought this through? Maybe what you actually want is the biggest member ID? That's one line of code: $last_member_id = $this->db->query('SELECT MAX(id) FROM member_data')->fetchColumn(); ok, i wil try your code. thx very much. Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480083 Share on other sites More sharing options...
Sky_Sailor Posted May 19, 2014 Author Share Posted May 19, 2014 If you're new to PDO, the best place to start is the PHP manual... http://www.php.net/manual/en/book.pdo.php yes, i have read all of that, but i still can't understand. i need people to teach me. is that why i'm joining here.. i hope you can help me to learn php here.. thx a lot Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480084 Share on other sites More sharing options...
Sky_Sailor Posted May 21, 2014 Author Share Posted May 21, 2014 hey anybody.. anyone can help me? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480243 Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 Help you with what? What's your question? Or did you expect us to write you a personal PDO tutorial? I don't think that's how this forum works. If you have a concrete question, just ask. But you won't get private PHP lessons for free or something. Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480246 Share on other sites More sharing options...
Sky_Sailor Posted May 21, 2014 Author Share Posted May 21, 2014 on model page :public function create($abc){ $insert = $this->db->prepare('INSERT INTO member_data (`member_id`,`username`,`name`, `password`,`address`,`phone`,`nik_numb`,`gender`,`role`) VALUES(:member_id, :username, :name, :password, :address, :phone, :nik_numb, :gender, :role) '); $insert->execute(array( $id = $this->db->lastInsertId(); ':member_id' => $abc['member_id'], ':username' => $abc['username'], ':name' => $abc['name'], ':password' => $abc['password'], ':address' => $abc['address'], ':phone' => $abc['phone'], ':nik_numb' => $abc['nik_numb'], ':gender' => $abc['gender'], ':role' => $abc['role'] )); ........and controllers page : $abc['member_id'] = 0 . date('ymdN') . '00' . $id;but i still don't get last insert id... please correct what is wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480248 Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 Please use code tags (the “< >” in the toolbar above the textarea). What is the $id = $this->db->lastInsertId(); line doing in the middle of the array expression? Is this actually how your code looks like? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480249 Share on other sites More sharing options...
IanA Posted May 21, 2014 Share Posted May 21, 2014 Shouldn't you be calling lastInsertId() following the execution of a query? And as Jacques1 said, it doesn't seem clear as to why you've placed that line of code within the array expression? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480259 Share on other sites More sharing options...
Sky_Sailor Posted May 27, 2014 Author Share Posted May 27, 2014 Shouldn't you be calling lastInsertId() following the execution of a query? And as Jacques1 said, it doesn't seem clear as to why you've placed that line of code within the array expression? i really didn't know what to do. i'm very confuse right now.. Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480981 Share on other sites More sharing options...
IanA Posted May 27, 2014 Share Posted May 27, 2014 What I am referring to is the execution of this line of code $id = $this->db->lastInsertId(); It should be ran following the execution of: $insert->execute(array( $id = $this->db->lastInsertId(); //REMOVE THIS LINE ':member_id' => $abc['member_id'], ':username' => $abc['username'], ':name' => $abc['name'], ':password' => $abc['password'], ':address' => $abc['address'], ':phone' => $abc['phone'], ':nik_numb' => $abc['nik_numb'], ':gender' => $abc['gender'], ':role' => $abc['role'] )); //AND PLACE IT HERE Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1480992 Share on other sites More sharing options...
Sky_Sailor Posted May 30, 2014 Author Share Posted May 30, 2014 What I am referring to is the execution of this line of code $id = $this->db->lastInsertId(); It should be ran following the execution of: $insert->execute(array( $id = $this->db->lastInsertId(); //REMOVE THIS LINE ':member_id' => $abc['member_id'], ':username' => $abc['username'], ':name' => $abc['name'], ':password' => $abc['password'], ':address' => $abc['address'], ':phone' => $abc['phone'], ':nik_numb' => $abc['nik_numb'], ':gender' => $abc['gender'], ':role' => $abc['role'] )); //AND PLACE IT HERE i have try that, but still didn't work.. ;'( Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1481444 Share on other sites More sharing options...
Jacques1 Posted May 31, 2014 Share Posted May 31, 2014 So what are we supposed to do now? We can't see your screen from here. If you want us to help you, then you need to post the code and describe the problem. No, “doesn't work” is not a valid problem description. In fact, we knew that already, because otherwise you probably wouldn't be here. So what's the problem? Does the INSERT query succeed at all? What's the return value of lastInsertId()? And why do you even want the last insertion ID? Don't you set it manually with $abc['member_id']? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1481454 Share on other sites More sharing options...
Sky_Sailor Posted May 31, 2014 Author Share Posted May 31, 2014 honestly, i really want to explain here.. but my english not really good. how if i post all files project here?? Quote Link to comment https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/#findComment-1481464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.