undefined offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tom91
Forum Newbie
Posts: 8
Joined: Thu Sep 01, 2005 3:10 pm

undefined offset

Post by tom91 »

Hi,
the following code:

Code: Select all

<html>
<body>
<centre><big><big>My Sightings</big></big></centre>
<br>
<br>
<table style="text-align: left; width: 156px; height: 32px;"
 border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="font-weight: bold; text-decoration: underline;">Number</td>
      <td style="font-weight: bold; text-decoration: underline;">Staus</td>
      <td style="font-weight: bold; text-decoration: underline;">Date
Sighted</td>
      <td style="font-weight: bold; text-decoration: underline;">Location
Sighted</td>
      <td style="font-weight: bold; text-decoration: underline;">Depot</td>
      <td style="font-weight: bold; text-decoration: underline;">Pool
Code</td>
      <td style="font-weight: bold; text-decoration: underline;">Livery</td>
      <td style="font-weight: bold; text-decoration: underline;">Owner</td>
      <td style="font-weight: bold; text-decoration: underline;">Name</td>
    </tr>

<?php

$host = "localhost";
$user = "root";
$pass = "*****";
$db = "tops";
$user = $_GET['user'];

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM sightings
WHERE user='$user' ORDER BY 'number'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
     // yes
     // print them one after another

     while(list($sightid, $user, $sightnumber, $day, $month, $year, $sightlocation)  = mysql_fetch_row($result)) {
$fullsightdate = "$day/$month/$year";

/*
-------------------------------------------------------------------------------------------------
*/
// create query
$queryloco = "SELECT * FROM dloco
WHERE number='$sightnumber'";

// execute query
$resultloco = mysql_query($queryloco) or die ("Error in query: $queryloco. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($resultloco) > 0) {
     // yes
     // print them one after another

     while(list($id, $class, $number, $status, $previous, $name_notes, $depot, $livery, $pool, $owner, $notes)  = mysql_fetch_row($result)) {


          echo "


    <tr>
      <td>$number</td>
      <td>$status</td>
      <td>$fullsightdate</td>
      <td>$sightlocation</td>
      <td>$depot</td>
      <td>$pool</td>
      <td>$livery</td>
      <td>$owner</td>
      <td>$name_notes</td>
    </tr>

";

     }
     }
}
}
else {
     // no
     // print status message
     echo "No rows found!";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>
Is returning the following error codes:
Notice: Undefined offset: 10 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 9 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 8 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 7 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 10 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 9 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 8 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 7 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 10 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 9 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 8 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 7 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 10 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 9 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 8 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

Notice: Undefined offset: 7 in C:\server\Apache2\htdocs\tops\mysightings.php on line 68

I don't really know what Undefined offset means and i cant find an explanation anywhere else. Any ideas why it's returning this?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What's line 68? That error means you're referring to an array element that hasn't been declared. My error reporting isn't that picky on my server, so I never get this error. However, I think this is an example of what would generate that error:

Code: Select all

$my_array['test_index'] = 'BOOYAH!';

This, I think, is the way to fix it.

Code: Select all

$my_array['test_index'];
$my_array['test_index'] = 'BOOYAH!';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes it means you're trying to refer to an array element before the array element even exists.... pickle's way won't fix it however and it wouldn't error on assignment like the above example shows.

I can't see which line would cause that in your code you posted but look for line 68 (probably in a loop somewhere) and this should help...

Example error producer:

Code: Select all

if ($some_condition)
{
    echo $array[10]; //Doesn't really exist
}
Example fix:

Code: Select all

if ($some_condition)
{
    echo isset($array[10]) ? $array[10] : '';
}
Either that, or check if it's defined elsewhere and then set it to some default if not ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

two things, if I may..

You are looping within a loop, when both loops are for queries - try rearranging your SQL to use Joins, this will be much, much more efficient :)

Both queries (and thus both loops) are utilising the same variable names (i.e. both are using $result). Thus the 2nd time the 1st loop runs, it will be for a different result set to what is expected.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

pickle wrote:My error reporting isn't that picky on my server, so I never get this error.
That's poor developing, ignoring errors instead of fixing them :)

Fix errors, do not ignore!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Jenk wrote:
pickle wrote:My error reporting isn't that picky on my server, so I never get this error.
That's poor developing, ignoring errors instead of fixing them :)

Fix errors, do not ignore!
its not an error, its a notice! Image
tom91
Forum Newbie
Posts: 8
Joined: Thu Sep 01, 2005 3:10 pm

Post by tom91 »

ok, line 68 is as follows:

Code: Select all

while(list($id, $class, $number, $status, $previous, $name_notes, $depot, $livery, $pool, $owner, $notes)  = mysql_fetch_row($result)) {
I'm now off to go and learn about sql joins. can anyone point me the direction of a good site on the subject?

Thanks for all your help.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Pimptastic wrote:
Jenk wrote:
pickle wrote:My error reporting isn't that picky on my server, so I never get this error.
That's poor developing, ignoring errors instead of fixing them :)

Fix errors, do not ignore!
its not an error, its a notice! Image
It's still an error. If FireFox didn't have this stupid Clipboard bug, I would copy and paste the url to the error pages on php.net, but alas I'm stuck with this gay bug.

What happens if the service provider suddely decides to turn E_ALL error reporting on? The site is full of ugly notice errors. :)

Nice smiley by the way :P
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

It's a notice Image

i am agreeing with you though, it is bad practice
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Then why does it appear on the PHP errors page?

It's an error. Hence it being prefixed with E_NOTICE, for ERROR_NOTICE. ;)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Image
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

either way i believe it's there to let you know that something might be wrong, so i wouldn't ignore them
it's easy enough to develop with them on, why do so otherwise?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

notice = error without any usual consequences.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the reason for the notices: $livery, $pool, $owner, $notes are not in the resultant rows returned via mysql_fetch_row()
Post Reply