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
class News {
        static $table_name = "news"; 
	static $table_fields = array(); 
	static $fields = array(); 
}

Open in new window

$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"=>"")

Open in new window

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.
LVL 2
pixalaxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
mysql_insert_id() will grab the ID from the last query run on the current connection, so you wont have issues with different users grabbing the wrong ID. Just make sure you grab the ID straight after the INSERT. Also, make sure your ID field in your table is auto-increment


0
pixalaxAuthor Commented:
Hello ChrisStanyon,
 Thank you for your reply. I hope it is the same for class variables. Do you have any idea about them?
0
Chris StanyonWebDevCommented:
Sorry pixalax - not really up on OOP in PHP.

Something I really ought to get my head around when I can find the time :)


0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

pius_babbunCommented:
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

?>

Open in new window

0
pixalaxAuthor Commented:
@pius_babbun;
 I have the function, I use PDO.
public static function lastInsertID () {
		global $db;
		return $result = $db->handler->lastInsertId();
	}

Open in new window

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).
0
Chris StanyonWebDevCommented:
@pixalax:

I've had a quick look around and I'm pretty sure you'll be OK - classes in PHP would be unusable if they shared data amongst all users.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pius_babbunCommented:
Yes pixalax, It should work as the same logic for last insert id ...but when you say you use PDO  ...offcourse it has some restrictions I hope ... Please refer this following link if it is helpful to you ..

http://www.php.net/manual/en/pdo.lastinsertid.php


and lastInsertId(); is variable or a property defined in the PDO class

string lastInsertId ([ string $name = NULL ] )

anyhow please check the link above whether it is useful to you
0
pixalaxAuthor Commented:
Thank you all for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.