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:)
How to access a newly created MySQL row entry?
Moderator: General Moderators
-
whamsicore
- Forum Newbie
- Posts: 8
- Joined: Thu Sep 30, 2010 4:18 pm
- 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?
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?
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?
My code
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?
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
}
}