I have a situation where i have to programmatically do database inserts. I have multiple tables but the order of information of those tables are similar , i.e, in each table, the first coulmn in id, and second is a foregin key, and third is a name, and fourth is a blob and fifth is a text.
I searched php doc and found that I can use $objectName[index] to access the database property. I am getting error
Cannot use object of type stdClass as array in C:\....php on line .. .
The erroneous line is indicated on the code
private function uploadTemp($databaseObject, $table_name){
$this->load->database();
//get file_contents too;
$file_id = $databaseObject[3]; // < Here's where the error appeared
$this->db->from('tbl_file')->where('file_id',$file_id);
$q = $this->db->get();
$data = $q->row();
$query = "INSERT INTO $table_name VALUES(NULL, '".$databaseObject[2]."','".$data->filecontent."');";
$this->db->query($query);
}
I am using CodeIgniter as a framework.
Try casting to array:
$file_id = (array) $databaseObject[3];
As STDClass is just a dummy container with public dynamic variables and no methods, there should be no problems in casting it to array and backwards as well.
However in some situations numbers are used to represent a variable name.
Example:
$array ; //Is some array created by engines / database handlers.
$obj = (object) $array ;
echo $obj->0 ; //Hell it will not work.
echo $obj[0] ; //The same problem.
echo $obj->{'0'} ; //PERFECT
try this function get_object_vars()
or create function to convert to array
function array_to_object($array)//from CodeIgniter forum
{
return (is_array($array)) ? (object) array_map(__FUNCTION__, $array) : $array;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With