pixalax
asked on
Class variables and last Insterted ID
Hello all,
I have a question about PHP and classes which made me think today. I'm coding my own small framework for my needs. My class looks like this
Let's say we have following columns : ID, Title, MsgBody, author_id, EnteryDate
So $fields is like this
My question starts here. Let's say I have 100 people adding the news at the same time, will this create any problems? I mean messing up with the data it would collect?
Another question is I can get last inserted ID from Database but is it related to my last action or is it related to last inserted ID of the data?
Let's say I will also send it as an e-mail after adding the data and I also want to write URL of the news in the e-mail such as www.mywebsite.com/news/?id=100.
After adding data to database, if I will use last insert ID to get the last inserted ID to collect the data. What is bugging me is, if I have 100 people writing news, before my code will get the last ID of the news I added, what if one of the authors will add another news? Will it take my news ID or other author's news ID?
I'm sorry, if I couldn't explain any better, didn't sleep for 48 hours, I'm having hard times to keep my eyes open. After making last changes (depending on you experts information) I'm planing to go and have a good sleep.
Thank you for your help and concern in advance.
I have a question about PHP and classes which made me think today. I'm coding my own small framework for my needs. My class looks like this
class News {
static $table_name = "news";
static $table_fields = array();
static $fields = array();
}
$fields is an array which is taking all column names from news database table.Let's say we have following columns : ID, Title, MsgBody, author_id, EnteryDate
So $fields is like this
$fields = array("ID"=>"", "Title"=>"", "MsgBody"=>"", "author_id"=>"", "EnteryDate"=>"")
When I submit the news form to add a news my framework takes values from news form and assigning them to $fields array value of related array keys.My question starts here. Let's say I have 100 people adding the news at the same time, will this create any problems? I mean messing up with the data it would collect?
Another question is I can get last inserted ID from Database but is it related to my last action or is it related to last inserted ID of the data?
Let's say I will also send it as an e-mail after adding the data and I also want to write URL of the news in the e-mail such as www.mywebsite.com/news/?id=100.
After adding data to database, if I will use last insert ID to get the last inserted ID to collect the data. What is bugging me is, if I have 100 people writing news, before my code will get the last ID of the news I added, what if one of the authors will add another news? Will it take my news ID or other author's news ID?
I'm sorry, if I couldn't explain any better, didn't sleep for 48 hours, I'm having hard times to keep my eyes open. After making last changes (depending on you experts information) I'm planing to go and have a good sleep.
Thank you for your help and concern in advance.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Sorry pixalax - not really up on OOP in PHP.
Something I really ought to get my head around when I can find the time :)
Something I really ought to get my head around when I can find the time :)
Hope you have method (function) in the class to insert the values to DB . After each of this insertion you can just return the mysql_last_insertid and store in a php variable . for example if you have method to insert in DB with following code like
<?php
//SAMPLE CODE TO GET THE LAST INSERTED ID
class Nameoftheclass
{
function Nameoftheclass()//CONTRUCTOR
{
}
function insert_function($form_value)//METHOD TO INSERT
{
$query = "INSERT INTO tablename (feildname) values ('".$form_value."')";
mysql_query($query) or die("Error : ".mysql_error());
return mysql_insert_id();
}
}
$objecName = new Nameoftheclass();//CREATE OBEJECT FOR THE CLASS
$last_insert_id = $objecName->insert_function($form_value);//CALL INSERT METHOD AND GET THE LAST INSERTED ID
?>
ASKER
@pius_babbun;
I have the function, I use PDO.
I have the function, I use PDO.
public static function lastInsertID () {
global $db;
return $result = $db->handler->lastInsertId();
}
This is the question which ChrisStanyon already answered, all I need to know about class variables if they are the same as last inserted id. I think it should be the same logic, anyway I coded way too far to make huge change (hopefully it won't come to that point).
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you all for your help.
ASKER
Thank you for your reply. I hope it is the same for class variables. Do you have any idea about them?