I have json file that contains of 50 lines of similar
"results":[{"score":0,"team_id":126266},{"score":0,"team_id":125798}].
I need to insert this to database, but I keep getting errors:
Notice: Undefined index: results[0]:score[0] in D:\xampp\htdocs\json\index.php on line 26
Notice: Undefined index: results[1]:score[0] in D:\xampp\htdocs\json\index.php on line 27
Notice: Undefined index: results[0]:team_id[0] in D:\xampp\htdocs\json\index.php on line 32
Notice: Undefined index: results[1]:team_id[0] in D:\xampp\htdocs\json\index.php on line 33
Here's the code:
$pirmo_oponento_rezultatas = $row['results[0]:score[0]'];
$antro_oponento_rezultatas = $row['results[1]:score[0]'];
$fk_Komandosid_Komandos = $row['results[0]:team_id[0]'];
$fk_Komandosid_Komandos1 = $row['results[1]:team_id[0]'];
$sql="INSERT INTO rungtynes (pirmo_oponento_rezultatas,antro_oponento_rezultatas,fk_Komandosid_Komandos,
fk_Komandosid_Komandos1) VALUES ('$pirmo_oponento_rezultatas','$antro_oponento_rezultatas',
'$fk_Komandosid_Komandos','$fk_Komandosid_Komandos1')";
You can access as below
$pirmo_oponento_rezultatas = $row['results'][0]['score'];
$antro_oponento_rezultatas = $row['results'][1]['score'];
Full error message:
Notice: Undefined variable: filter in
/srv/www/htdocs/test/tiltangle/dpd/query.php on line 104
line 104:
if (!($b1=="")) $filter=$filter." and b>=$b1";
line 35:
$b1=$_POST["b1"];
As you see it was defined.
I could imagine that your code looks like this:
if(...) {
$b1=$_POST["b1"];
}
...
if (!($b1=="")) $filter=$filter." and b>=$b1";
So it is possible that the first if-Condition is false, so $b1 is never set.
I'm trying to fetch the name and artists of all the tracks on a spotify playlist.
I have no problem fetching the name of the playlist, the coverart, the total count of followers.
But when I try to fetch the name and artist i get this line of errors
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Undefined property: stdClass::$name in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Undefined property: stdClass::$name in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
my code is
$string = file_get_contents("playlist.json");
$json=json_decode($string);
echo $json->followers->total.'<br />';
foreach($json->images as $image){
echo '<img src="'.$image->url.'" /><br>';
}
echo $json->name.'<br />';
$i = 0;
foreach($json->tracks->items as $tracks){
foreach($tracks->track as $track) {
echo($track->name);
};
if(++$i > 1) break;
};`
And my JSON is this
http://wecre8.it/playlist.json
I can't find the problem, and I tried google.
Hope someone can find my error and help fetch the artist and titles of the tracks in the playlist
In your example JSON, the track object is under the items array and not under tracks object as what your code is looking for.
$string = file_get_contents ( 'playlist.json' );
$array = json_decode ( $string, TRUE );
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Not sure why I'm getting this error as the code is working and I can see the data I expected.
Code
$allSales = array();
foreach ($game['sales'] as $sale) {
foreach ($sale['values'] as $id => $values) {
$allSales[$id]+=$values['y'];
}
}
Error 1
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: player/game.php
Line Number: 81
Error 2 (Same Code)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: player/game.php
Line Number: 81
The statement:
$allSales[$id] += $values['y'];
means to take the current value of $allSales[$id], add the value of $values['y'] to it, and store that result back in $allSales[$id]. But the first time that you encounter a particular $id, there is no $allSales[$id]. This results in the warning when you try to get its current value.
Change to:
if (isset($allSales[$id])) {
$allSales[$id] += $values['y'];
} else {
$allSales[$id] = $values['y'];
}
Or you could just prefix the line with # to suppress warnings (I know purists will cry over this):
#$allSales[$id] += $values['y'];
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
I'm receiving the following errors when I run my PHP script. The script is supposed to do some mapping in the database via JOINs, and then export the results to a CSV file.
Notice: Undefined index: partterminologyname in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 358
Notice: Undefined index: YearID in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 359
Notice: Undefined index: MakeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 360
Notice: Undefined index: modelname in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 361
Notice: Undefined index: SubModelName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 362
Notice: Undefined index: EngineDesignationName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 363
Notice: Undefined index: EngineVINName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 364
Notice: Undefined index: Liter in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 365
Notice: Undefined index: CC in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 366
Notice: Undefined index: CID in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 367
Notice: Undefined index: Cylinders in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 368
Notice: Undefined index: BlockType in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 369
Notice: Undefined index: EngBoreIn in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 370
Notice: Undefined index: EngBoreInMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 371
Notice: Undefined index: EngStrokeIn in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 372
Notice: Undefined index: EngStrokeMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 373
Notice: Undefined index: FuelDeliveryTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 374
Notice: Undefined index: FuelDeliverySubTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 375
Notice: Undefined index: FuelSystemControlTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 376
Notice: Undefined index: FuelSystemDesignName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 377
Notice: Undefined index: AspirationName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 378
Notice: Undefined index: CylinderHeadTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 379
Notice: Undefined index: FuelTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 380
Notice: Undefined index: IgnitionSystemTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 381
Notice: Undefined index: EngineMfr.MfrName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 382
Notice: Undefined index: EngineVersion in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 383
Notice: Undefined index: ValvesPerEngine in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 384
Notice: Undefined index: BedLength in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 385
Notice: Undefined index: BedLengthMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 386
Notice: Undefined index: BedTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 387
Notice: Undefined index: BodyNumDoors in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 388
Notice: Undefined index: BrakeSystemName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 389
Notice: Undefined index: BrakeABSName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 390
Notice: Undefined index: BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 391
Notice: Undefined index: BrakeFront.BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 392
Notice: Undefined index: BrakeRear.BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 393
Notice: Undefined index: DriveTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 394
Notice: Undefined index: MfrBodyCodeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 395
Notice: Undefined index: SpringTypeFront.SpringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 396
Notice: Undefined index: SpringTypeRear.SpringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 397
Notice: Undefined index: SteeringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 398
Notice: Undefined index: TransmissionTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 399
Notice: Undefined index: TransmissionNumSpeeds in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 400
Notice: Undefined index: TransmissionControlTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 401
Notice: Undefined index: TransmissionMfrCode in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 402
Notice: Undefined index: TransmissionMfr.MfrName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 403
Notice: Undefined index: ElecControlled in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 404
Notice: Undefined index: WheelBase in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 405
Notice: Undefined index: WheelBaseMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 406
Notice: Undefined index: VehicleTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 407
The code related to this is as follows...
// Write all the user records to the spreadsheet
/* fetch associative array */
while ($row = $results->fetch_assoc()) {
fputcsv($handle, array(
'admin',
'base',
'Default',
'simple',
'2',
$brandprefix . $row['part_id'],
$row['part_id'],
'1',
'1',
'Enabled',
'Description',
'Short Description',
$row['partterminologyname'],
$row['YearID'],
$row['MakeName'],
$row['modelname'],
$row['SubModelName'],
$row['EngineDesignationName'],
$row['EngineVINName'],
$row['Liter'],
$row['CC'],
$row['CID'],
$row['Cylinders'],
$row['BlockType'],
$row['EngBoreIn'],
$row['EngBoreInMetric'],
$row['EngStrokeIn'],
$row['EngStrokeMetric'],
$row['FuelDeliveryTypeName'],
$row['FuelDeliverySubTypeName'],
$row['FuelSystemControlTypeName'],
$row['FuelSystemDesignName'],
$row['AspirationName'],
$row['CylinderHeadTypeName'],
$row['FuelTypeName'],
$row['IgnitionSystemTypeName'],
$row['EngineMfr.MfrName'],
$row['EngineVersion'],
$row['ValvesPerEngine'],
$row['BedLength'],
$row['BedLengthMetric'],
$row['BedTypeName'],
$row['BodyNumDoors'],
$row['BrakeSystemName'],
$row['BrakeABSName'],
$row['BrakeTypeName'],
$row['BrakeFront.BrakeTypeName'],
$row['BrakeRear.BrakeTypeName'],
$row['DriveTypeName'],
$row['MfrBodyCodeName'],
$row['SpringTypeFront.SpringTypeName'],
$row['SpringTypeRear.SpringTypeName'],
$row['SteeringTypeName'],
$row['TransmissionTypeName'],
$row['TransmissionNumSpeeds'],
$row['TransmissionControlTypeName'],
$row['TransmissionMfrCode'],
$row['TransmissionMfr.MfrName'],
$row['ElecControlled'],
$row['WheelBase'],
$row['WheelBaseMetric'],
$row['VehicleTypeName']
));
}
I tried to echo one of the row values and then issue a die();, and the value echoed properly. I'm wondering if I'm missing something here... thanks!
Before you fetch the array, you must be validate if the record has some value:
if (count($row) > 1) {
//Fetch the items
}
The most common reason for this error, is because you are trying to use a data that doesn't exists.
Don't forget initialize your variables too.
$row = array()
Greetings