解析したデータをデータベースのテーブルに挿入しようとすると、次のエラーが発生します。
PHP Notice: Undefined index: manu in /home/svn/dev.comp/Asus.php on line 112
PHP Notice: Undefined index: partnumber in /home/svn/dev.comp/Asus.php on line 112
PHP Notice: Undefined index: description in /home/svn/dev.comp/Asus.php on line 112
PHP Notice: Undefined property: DatabaseManager::$_link in /home/svn/dev.comp/Asus.php on line 112
PHP Warning: mysql_query() expects parameter 2 to be resource, null given in /home/svn/dev.comp/Asus.php on line 112
ここに私がこれまでに持っているコードがあります:
<?php
//Declaring all database connection information
$username = "username";
$password = "password";
$database = "database_name";
//Setting connection to database, fail if could not connect
$link = mysql_connect("localhost", $username, $password) or die("Could not connect to database");
mysql_select_db($database, $link);
$csv = new CSVManager();
$data = $csv->get_data_array();
$api = new DataGrabber($link);
$xmldata = $api->call_api($data);
$dm = new DatabaseManager();
$dm->insert_asus_data($xmldata);
//This class will pull data from the .CSV file and store it inside an array
class CSVManager {
public function get_data_array() {
//Open and declare location of .CSV file to pull data from
$hook = fopen('asus.csv', 'r');
//Number of columns in .CSV file we are getting data from
$allowedCols = 5;
//Store all data we will extract, into an array
$data = array();
//Count the number of lines in the .CSV file
while($line = fgetcsv($hook)) {
$numcols = count($line);
//Test wether the number of columns we are declaring, is equal to the number of columns in the .CSV file
if($numcols == $allowedCols) {
//If so, assign each data row from the column names to new entries which will be declared in the DatabaseManager Class
$entry = array(
'man' => $line[0], //index of column name in .CSV file
'cat' => $line[1],
'model' => $line[2],
'family_id' => $line[3],
'family' => $line[4]
);
//Insert all entries into the data array declared above
$data[] = $entry;
}
}
//Once data is inside the array, return this data
return $data;
}
}
//This class will pull data from the XML document and store it inside another array
class DataGrabber {
//The URL where data will be extracted from, which is an XML file
protected $URL = "http://json.zandparts.com/api/category/GetCategories/44/EUR/";
public function call_api($data) {
if(count($data) == 0) return array();
$jsondata = array();
foreach($data as $entry){
$url = $this->URL . $entry['model'] . "/". "E%20Series" . "/" . rawurlencode($entry['cat']) . "/" . $entry['man'] . "/null";
$json = file_get_contents($url);
$data = json_decode($json, true);
if(!empty($data['Products'])){
foreach ($data['Products'] as $id => $product) {
$jsonentry = array(
'productnumber' => $id,
'partnumber' => $product['strPartNumber'],
'description' => $product['strDescription'],
'manu' => $product['Brand']
);
$jsondata[] = $jsonentry;
}
}
}
return $jsondata;
}
}
ここまでは問題なく動作しますが、以下のコードでは上記のエラーがスローされます。
class DatabaseManager {
public function insert_asus_data($partbind) {
//create a new MySQL statement to insert data into the database table. Bind the entries declared above from both classes, to the fieldnames in the database and insert them as values.
mysql_query("INSERT INTO asus_parts (manufacturer, partNumber, description) VALUES ('{$partbind['manu']}', '{$partbind['partnumber']}','{$partbind['description']}')", $this->_link);
}
}
?>
それらは両方とも同じファイルにあります..なぜこれらのエラーがスローされるのかわかりません。どうすればこれを克服できますか? ありがとう。
[編集]
以下は、印刷ステートメントの 1 つです。
[5] => Array
(
[productnumber] => 0
[partnumber] => 0B200-00120100
[description] => ME370T BAT COSLI LI-POLY FPACK
[manu] => Google
)
[6] => Array
(
[productnumber] => 1
[partnumber] => 0B200-00120200
[description] => ME370T BAT COSLI LI-POLY FPACK
[manu] => Google
)
まだテーブルにデータを挿入しません