mysql_fetch_array not creating right elements?

63 views Asked by At

I am getting the following error message:

Notice: Undefined index: timein in time.php on line 42 Notice: Undefined index: timeout in time.php on line 45

Here is my code:

if (isset($_POST['submit'])) {
    $name = $_POST['name'];

$querytimeout = mysql_query("
    SELECT timein 
    FROM studentInfo
    WHERE name = '$name' 
    ORDER BY time DESC
    LIMIT 1,1
    ")
    or die("Error querying database ".mysql_error());
    $querytimein = mysql_query("
    SELECT timeout
    FROM studentInfo
    WHERE name = '$name' 
    ORDER BY time DESC
    LIMIT 1
    ")
    or die("Error querying database ".mysql_error());
while($minutestimein = mysql_fetch_array($querytimein)){
    $ltimein = $minutestimein['timein'];
    }
        while($minutestimeout = mysql_fetch_array($querytimeout)){
    $ltimeout = $minutestimeout['timeout'];
  }
  $timegone = $ltimein - $ltimeout; 
  echo $timegone;
  }

The problem is that mysql_fetch_array() isn't making timein or timeout an element of the array $minutestimein.

2

There are 2 answers

2
Mike Brant On

If you compare your queries to the fields that they are looking up, they don't match.

In the first query you select only timeout yet you try to reference $minutestimein['timein']. You do the opposite in the second query.

0
Marc B On

Your two queries are redundant - they're fetching two different fields from the SAME record. Why not just run ONE query?

SELECT timein, timeout
FROM studentInfo
WHERE name = '$name'
ORDER BY time DESC
LIMIT 1

then

$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$diff = $row['timeout'] - $row['timeout'];

Plus, there is literally NO point in using a while() loop to fetch your results. You've already limited your queries to returning only ONE row anyways, making the entire looping infrastructure useless.