Page 1 of 1

How to access a newly created MySQL row entry?

Posted: Thu Sep 30, 2010 4:47 pm
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:)

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

Posted: Thu Sep 30, 2010 5:36 pm
by Bill H
Well, you're not going to be able to access anything from a database until you fetch it.

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

Posted: Thu Sep 30, 2010 6:12 pm
by Eran
You should post your code

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

Posted: Thu Sep 30, 2010 8:09 pm
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?