How to access a newly created MySQL row entry?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
whamsicore
Forum Newbie
Posts: 8
Joined: Thu Sep 30, 2010 4:18 pm

How to access a newly created MySQL row entry?

Post by whamsicore »

I'm currently working on a project where I need to display an entry from MySQL for every day of the month. If the database doesn't have an entry for a particular day, it is supposed to create an entry and immediately display it, along with everything else.

Problem: PHP doesn't seem to be able to access the record/entry immediately created. The database entries are being created correctly, only I can't seem to be able to access them immediately (I can access them fine if I run my fetch again, through the browser, but not in the same instance). I am stuck.

P.S: Forwarding a thanks to anybody who can help:)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: How to access a newly created MySQL row entry?

Post by Bill H »

Well, you're not going to be able to access anything from a database until you fetch it.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How to access a newly created MySQL row entry?

Post by Eran »

You should post your code
whamsicore
Forum Newbie
Posts: 8
Joined: Thu Sep 30, 2010 4:18 pm

Re: How to access a newly created MySQL row entry?

Post by whamsicore »

My code

Code: Select all

public static function find_by_date($day="2010-09-29", $day_part=1){ 
		global $database;
		$sql = "SELECT * FROM ".static::$table_name;
		$sql .= " WHERE day='".$database->escape_value($day);
		$sql .= "' AND day_part=".$database->escape_value($day_part);
		$sql .= " LIMIT 1";
		
		$result_array = static::find_by_sql($sql);
		
		if(!empty($result_array)){
			return array_shift($result_array);
		}else{
			$new_entry= new video; //video is my class, it has day and day_part properties, as well as comments and description which will be set with placeholders for now
                        $new_entry->day=$day;
                        $new_entry->day_part=$day_part
                        $new_entry->create(); //Creates placeholder entry for designate date
			//new MySQL entry is already made 
			static::find_by_date($day,$day_part); //attempts to retrieve newly created entry: and fails
		}
	}	
Explain: The find_by_sql function will handle the fetch array part. If the result_array is not set then it means the data base entry does not exist. In response, I will create the corresponding data base entry with placeholder values. Once it is created I will call the find_by_date function again. However this does not work. If I run the whole page again it WILL work. Seems like PHP is not capable of accessing newly created MySQL entries immediately. Is there some way around this?
Post Reply