PHP mysqli_fetch_array missing some fields - php

I have crated a database that stores information about boats, when I tried to use this data in PHP it worked, then I added some new fields to the database but when I use them in PHP they return "Undefined index: fieldName".
From what I know it means that the index has not been set, so I checked the contents of the array to find that all of the fields are there except the new ones, however i am able to view them in phpmyadmin.
PHPMyAdmin:
See Image: https://dl.dropboxusercontent.com/u/65222600/phpmyadminshot.png
PHP Code:
<?php
include_once "php/connect.php";
$result = mysqli_query($con,"SELECT * FROM vessels");
while($row = mysqli_fetch_array($result)){
print_r( $row );
}
?>
PHP Returns:
Array (
[0] => 1 [idVessels] => 1
[1] => 1 [userId] => 1
[2] => cams boat [name] => cams boat
[3] => 1000000000 [price] => 1000000000
[4] => 0 [VAT] => 0
[5] => GBP [Currency] => GBP
[6] => UK [Location] => UK
[7] => ME [builder] => ME
[8] => ME [make] => ME
[9] => ME [model] => ME
[10] => 10/10/13 [yearConstructed] => 10/10/13
[11] => 1 [cabins] => 1
[12] => 1 [heads] => 1
[13] => 1 [#engines] => 1
[14] => ME [engineModel] => ME
[15] => 100 [enginePower] => 100
[16] => petrol [fuelType] => petrol
[17] => 122 [nominalLength] => 122
[18] => 122 [overallLength] => 122
[19] => 122 [waterlineLength] => 122
[20] => 122 [beam] => 122
[21] => 122 [maxDraft] => 122
[22] => leather [hullMaterial] => leather
[23] => MINT [hullType] => MINT
[24] => MINT [keeltype] => MINT
[25] => 122 [displacement] => 122
[26] => 122 [waterCapacity] => 122
[27] => 2014-01-10 02:18:39 [dateAdded] => 2014-01-10 02:18:39
[28] => For Sale [status] => For Sale )
and nowhere is description or primaryImageURL.
any help is greatly appreciated, Thanks

Thanks for all the help guys, turns out i was using the project DB not my Dev DB

Try to reference explicitly fields names:
$result = mysqli_query($con,"SELECT IdVessels, userId, name, description, primaryImageURL FROM vessels");
That way you'll be able to find the problem more quickly. If the db answer that the fields doesn't existe, probably you are pointing your connection to an old database.

Related

How to find the most occurring values from a collection?

Hi I have a collection of numbers and I want to find the most occurring number and then the 2nd most occurring and then the third most occurring till 10 and store the result in a array.
The collection looks like this
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => 12
[1] => 194
[2] => 241
[3] => 4
[4] => 29
[5] => 4
[6] => 12
[7] => 15
[8] => 21
[9] => 31
[10] => 281
[11] => 4
[12] => 6
[13] => 4
[14] => 2
[15] => 6
[16] => 4
[17] => 4
[18] => 4
[19] => 15
[20] => 4
[21] => 4
[22] => 13
[23] => 10
[24] => 8
[25] => 8
[26] => 2
[27] => 2
[28] => 2
[29] => 17
[30] => 4
[31] => 20
[32] => 2
[33] => 4
[34] => 20
[35] => 6
)
)
So I want to find the most occurring one and so on till 10th most occurring number.
use array_count_values, here is your reference link http://www.w3schools.com/php/func_array_count_values.asp
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )

PHP - How would one go about combining 3 different arrays into one

So i have 3 different ( yet they share some similarities ) and i would like to combine these into one to perform some calculations
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => Amber
[1] => 16
[3] => 2.0%
[4] => 0.2
[5] => 28.6
[6] => 1.8
[7] => 1861.1
[8] => 116.3
[9] => 0.0
[10] => 0.0
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 0.8%
[15] => 10
[16] => 6
[17] => 62.5%
[18] => 37.5%
)
[2] => Array
(
[0] => Amie
[1] => 106
[3] => 13.5%
[4] => 3.0
[5] => 721.7
[6] => 6.8
[7] => 12268.0
[8] => 115.7
[9] => 0.0
[10] => 0.0
[11] => 127011.0
[12] => 6350.5
[13] => 20.0
[14] => 9.7%
[15] => 54
[16] => 52
[17] => 50.9%
[18] => 49.1%
)
)
Array 2
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
)
Array 3
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => (7312
[1] => 1
[3] => 0.0%
[4] => 459.8
[5] => 0.0
[6] => 0.0
[7] => 0.4
[8] => 0.4
[9] => 0.0
[10] => 0.0
[11] => 7.8
[12] => 2.6
[13] => 3.0
[14] => 5.5%
[15] => 1
[17] => 100.0%
[18] => 0.0%
)
[2] => Array
(
[0] => Amber
[1] => 414
[2] => 9
[3] => 9.3%
[4] => 6.3
[5] => 1880.1
[6] => 4.4
[7] => 65209.8
[8] => 157.5
[9] => 240.4
[10] => 0.6
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 27.8%
[15] => 290
[16] => 124
[17] => 70.0%
[18] => 30.0%
)
So in this example i have 3 arrays, all have the same first initial array. But these arrays can differ in size and they may not share the same people in in.
People can also be in different places so i.e.
Array 1 : Common -> Amber -> Amie
Array 2 : Common ->
Array 3 : Common -> 7312 -> Amber ->Amie
Now the final array should look like
Array 4 : Common -> 7312 -> Amber -> Amie
All the values from all arrays should also be combined so i.e:
Amber 1st array / 1st value : 16
Amber 2nd array / 2nd value : 414
Amber final array value : 414 + 16 = 430
Hopefully i have made question clear.
Basically, you sum up all the info you got, remove the headers. Then rebuild a new array with the info consolidation.
//merge all arrays, remove the first element, it is headers.
$my_total_array = array_merge(
array_splice($array1,1),
array_splice($array2,1),
array_splice($array3,1)
);
$myagents = array();
foreach($my_total_array as $agent){
if(!isset($myagents[$agent[0]])){
//Never met the agent, add them.
$myagents[$agent[0]] = $agent;
}else{
//We already seen the agent, do maths.
$myagents[$agent[0]][1]+=$agent[1];
//TODO: Apply consolidation rules for other fields.
}
}
//Put the headings back at the beggining.
array_unshift($myagents,$array1[0]);
print_r($myagents);
I hope this helps.
EDIT: From PHP Docs
Merges the elements of one or more arrays together so that the values
of one are appended to the end of the previous one. It returns the
resulting array.
If the input arrays have the same string keys, then the later value
for that key will overwrite the previous one. If, however, the arrays
contain numeric keys, the later value will not overwrite the original
value, but will be appended.

Assigning values to array

I have the following array structure. with the variable $sortie
Array
(
[0] => 480
[1] => 483
[2] => 497
[3] => 481
[4] => 478
[5] => 475
[6] => 476
[7] => 477
[8] => 498
[9] => 496
[10] => 502
[11] => 499
[12] => 494
[13] => 503
[14] => 493
[15] => 500
[16] => 484
[17] => 501
[18] => 495
[19] => 485
[20] => 489
[21] => 490
[22] => 488
[23] => 487
[24] => 486
)
I'm trying to achieve something by assigning them on a new array using the following code.
$release = array();
foreach ($sortie as $key_true => $value_true) {
$release[$key_true] = $value_true;
echo $key_true.'---'.$value_true.'<br>';
}
So far the echo results are going as expected with the correct order based on $sortie
however the $release array is not following the same ordering not assigning the $key_true to the $release array. Would appreciate any help why it's doing this.
EDIT
Sorry almost forgot, result of $release the values are the original key and original value
Array
(
[0] => 0---480
[1] => 1---483
[2] => 10---502
[3] => 11---499
[4] => 12---494
[5] => 13---503
[6] => 14---493
[7] => 15---500
[8] => 16---484
[9] => 17---501
[10] => 18---495
[11] => 19---485
[12] => 2---497
[13] => 20---489
[14] => 21---490
[15] => 22---488
[16] => 23---487
[17] => 24---486
[18] => 3---481
[19] => 4---478
[20] => 5---475
[21] => 6---476
[22] => 7---477
[23] => 8---498
[24] => 9---496
)
The best practice is change array to this structure:
$sortie = [480, 483, ...];
$release = [];
foreach($sortie as $val) {
$release[$val] = true;
}
And then you can use php array sorting functions
Try putting in $release == array();, it may work around for you as far as assigning is concerned

Looping over Dynamic Multi-dimensional Array with PHP

I am having difficulty with an multidimensional array. Here is a portion of the array. It's dynamic in that the top level array can have any number of elements and the child arrays, can have any number of elements as well. But each child array will contain the same amount of elements. If one child array has 16 elements, they will all have 16 elements, and if one has 20 elements, they will all have 20 elements.
[1] => Array
(
[1] => 0
[2] => 25
[3] => 38
[4] => 50
[5] => 63
[6] => 75
[7] => 85
[8] => 88
[9] => 100
[10] => 113
[11] => 125
[12] => 138
[13] => 150
[14] => 163
[15] => 175
[16] => 188
)
[2] => Array
(
[1] => 48
[2] => 37.22
[3] => 52.56
[4] => 63.17
[5] => 74.45
[6] => 87.98
[7] => 96.11
[8] => 98.36
[9] => 111.67
[10] => 132.20
[11] => 146.87
[12] => 160.85
[13] => 174.39
[14] => 187.70
[15] => 203.04
[16] => 215.90
)
What I am trying to do is to extract the data in a format like:
ProductCode: [2][1][1][2], Width: [2][1], Height: [1][2], Price: [2][2].
Can someone point me in the right direction of how I can loop over this to extract the data like I need to?
I ended up creating a stored procedure in SQL 2008 to handle this instead of using PHP.

preg_match only 4 digits numbers

Im trying to get all the 4 digit numbers from this website, However there are some numbers that are less than 4 digits for example #20 in the array is value 20.
Array ( [0] => 6280 [1] => 6279 [2] => 6278 [3] => 6277 [4] => 6276 [5] => 6275 [6] => 6274 [7] => 6273 [8] => 6272 [9] => 6271 [10] => 6270 [11] => 6269 [12] => 6268 [13] => 6267 [14] => 6266 [15] => 6265 [16] => 6264 [17] => 6263 [18] => 6262 [19] => 6261 [20] => 20 [21] => 6320 )
This is the script im using:
$pattern = '#<b>([0-9]+)</b>#';
preg_match_all($pattern,$website,$match_number);
Is it possible to only get it if there are 4 digits no less no more.
Thanks
Change the pattern to:
$pattern = '#<b>([0-9]{4})</b>#';

Categories