5 Replies - 572 Views - Last Post: 09 October 2012 - 01:58 PM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 10:17 AM

Hey guys,
I am having a small issue. I have a script that reads spreadsheets, and executes a query to insert data into my Database. Unfortunately, when I run my query the field in the database reads ARRAY!! My array consists of spreadsheet columns. What am I doing wrong?

When I do a var_dump or a print r..I get the following....


Array ( [A] => Array ( [1] => Name ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) [G] => Array ( [1] => BrokerLastName ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) [G] => Array ( [1] => BrokerLastName ) [H] => Array ( [1] => BrokerSuffix ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) [G] => Array ( [1] => BrokerLastName ) [H] => Array ( [1] => BrokerSuffix ) [I] => Array ( [1] => BrokerLicense ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) [G] => Array ( [1] => BrokerLastName ) [H] => Array ( [1] => BrokerSuffix ) [I] => Array ( [1] => BrokerLicense ) [J] => Array ( [1] => Comments ) ) Array ( [A] => Array ( [1] => Name ) [B] => Array ( [1] => Premium ) [C] => Array ( [1] => EffectiveDate ) [D] => Array ( [1] => ExpirationDate ) [E] => Array ( [1] => BrokerFirstName ) [F] => Array ( [1] => BrokerMiddle ) [G] => Array ( [1] => BrokerLastName ) [H] => Array ( [1] => BrokerSuffix ) [I] => Array ( [1] => BrokerLicense ) [J] => Array ( [1] => Comments ) [K] => Array ( [1] => Created ) )


With that said, here is my code.

require_once 'CachedObjectStorageFactory.php';
require_once 'Settings.php';
require_once 'IOFactory.php';
// Cell caching to reduce memory usage.
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_in_memory;
$cacheSettings = array( ' memoryCacheSize ' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objPHPExcel = PHPExcel_IOFactory::load("spreadsheet.xls");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    $cells = array();
    $value = array_shift($cells);	
    $Sheet = $objPHPExcel->getActiveSheet();
     echo '<br><table border="1"><tr>';
    for ($row = 1; $row <= $highestRow; ++ $row) {
       echo '<tr>';
        for ($col = 0; $col <= $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getFormattedValue();
            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
            
	   $ExcelColumns = array(0=>'A',1=>'B',2=>'C',3=>'D',4=>'E',5=>'F',6=>'G',7=>'H',8=>'I',9=>'J',10=>'K');
          
        $cells[$ExcelColumns[$col]][$row] = $Sheet->getCell($ExcelColumns[$col].$row)->getValue();
        
        
        $slic = mysql_connect("host", "user", "password");
                      if(!$slic) {
                    die('Could not connect!' . mysql_error());
                    


                        }
        mysql_select_db("xoxox", $slic) or die (mysql_error()); 
        
        
        
        $dbc = mysql_query("INSERT INTO coverages (Name) VALUES ('$cells')") or die (mysql_error());
                
      

    mysql_close();

     } 	        	             		   	 		    			   
    echo '</tr>'; 
}
    echo '</table>';			
     			
     			
              
          }
           
      }
     



Is This A Good Question/Topic? 0
  • +

Replies To: Problem: Insert Into inputs (Array) in field in database

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 10:28 AM

$cells is an array, when you convert an array to a string, you always yield the string "Array". that is how the complex data type array is converted by PHP.

on the other hand side, ask yourself what values you expect to be in the SQL query and how to get them there from the array.
Was This Post Helpful? 1
  • +
  • -

#3 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 11:18 AM

I want the values from my spreadsheet to be in my query. Also the reason I am using the array is so I won't have to create a variable for each cell.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 01:13 PM

View PostBarNunBoi, on 09 October 2012 - 08:18 PM, said:

Also the reason I am using the array is so I won't have to create a variable for each cell.

But unfortunately SQL does not support PHP arrays.
Was This Post Helpful? 0
  • +
  • -

#5 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 01:31 PM

So are you telling me there is no way to work around this!?? Could I use the implode function?

This post has been edited by BarNunBoi: 09 October 2012 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: Problem: Insert Into inputs (Array) in field in database

Posted 09 October 2012 - 01:58 PM

View PostBarNunBoi, on 09 October 2012 - 10:31 PM, said:

So are you telling me there is no way to work around this!??

I only tell you, that you canít use an array as is.



View PostBarNunBoi, on 09 October 2012 - 10:31 PM, said:

Could I use the implode function?

for instance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1