Page 1 of 1

undefined offset

Posted: Mon Oct 17, 2005 10:29 am
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?

Posted: Mon Oct 17, 2005 10:46 am
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!';

Posted: Mon Oct 17, 2005 11:04 am
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 ;)

Posted: Mon Oct 17, 2005 11:20 am
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.

Posted: Mon Oct 17, 2005 11:22 am
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!

Posted: Mon Oct 17, 2005 11:24 am
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

Posted: Mon Oct 17, 2005 11:27 am
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.

Posted: Mon Oct 17, 2005 11:27 am
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

Posted: Mon Oct 17, 2005 11:29 am
by JayBird
It's a notice Image

i am agreeing with you though, it is bad practice

Posted: Mon Oct 17, 2005 11:30 am
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. ;)

Posted: Mon Oct 17, 2005 11:33 am
by JayBird
Image

Posted: Mon Oct 17, 2005 11:35 am
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?

Posted: Mon Oct 17, 2005 12:03 pm
by Skara
notice = error without any usual consequences.

Posted: Mon Oct 17, 2005 6:35 pm
by feyd
the reason for the notices: $livery, $pool, $owner, $notes are not in the resultant rows returned via mysql_fetch_row()