0

I got a function that requests some information from my database, puts it in an array and returns it. The sql-statement is correct.

When the current date is not found in the database, I would like to display an error on my website. The error message, is added also in the return array.

First I check if the corresponding date exists in the database with a COUNT, if the count == 1, I get all the data from the database with another statement. If the count != 1, I put together an array hard code.

When a date is found in the database that is the same as the given parameter, the script works like a charm, but when I change the date in the database, the I get the following error:

Notice: Undefined variable: row in /Applications/MAMP/htdocs/models/funcs.php on line 1257

Line 1252-1257:

while($stmt->fetch())
            {
                $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
            }
            $stmt->close();
            $data = $row;

Does anyone know what I'm doing wrong here? Thanks in advance!

The complete function:

function get_current_planning($date)
{
    GLOBAL $mysqli, $db_table_prefix;
    $stmt = $mysqli->prepare("SELECT COUNT(datum) FROM " . $db_table_prefix . "planning WHERE datum = '" . $date . "'");
    $result = $stmt->execute();
print_r($result);
var_dump($result);

    if($result == 1)
    {
        $stmt->prepare("SELECT
                                        *
                                        FROM " . $db_table_prefix . "planning
                                        WHERE datum = '" . $date . "'");
        $stmt->execute();
        $stmt->bind_result($id, $datum, $mac, $ipad, $iphone, $imember, $applecare, $verkoop_ochtend, $verkoop_middag, $verkoop_avond, $vracht_ochtend, $vracht_middag, $vracht_avond, $service_ochtend, $service_middag, $service_avond, $werkzaamheden);
        while($stmt->fetch())
            {
                $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
            }
            $stmt->close();
            $data = $row;
    }
    else
    {
        $row[] = array('id' => '', 'datum' => '', 'mac' => '', 'ipad' => '', 'iphone' => '', 'imember' => '', 'applecare' => '', 'verkoop_ochtend' => '', 'verkoop_middag' => '', 'verkoop_avond' => '', 'vracht_ochtend' => '', 'vracht_middag' => '', 'vracht_avond' => '', 'service_ochtend' => '', 'service_middag' => '', 'service_avond' => '', 'werkzaamheden' => '', 'error' => 'Er is geen planning gevonden voor de huidige datum!');
        $stmt->close();
        $data = $row;
    }
    return $data;
}

print_r($result) returns 1

var_dump($result) returns bool(true)

Jules
  • 546
  • 2
  • 11
  • 36
  • What if there are no rows? Since you're not defining `$rows` before your loop it would never exist (and be an *undefined* variable!) – h2ooooooo Feb 09 '15 at 18:27

2 Answers2

1

You're not fetch()ing the the result of your SELECT COUNT()... query. It looks to me like your while(...fetch()) loop in your second query is sometimes running zero times. This will result in the code beginning $row[] = getting run no times, which in turn will result in $row turning up undefined after the while(...fetch()) loop.

At any rate it's a bit wasteful to count the rows and then fetch them, with two consecutive queries. You may want to skip the SELECT COUNT query entirely. Instead, do this sort of thing:

$row = Array();
while($stmt->fetch()) {
    $row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
if (0 == count($row)) {
     /* deal with the no matching rows case */
}
$data = $row;
O. Jones
  • 103,626
  • 17
  • 118
  • 172
-1

It looks like you haven't defined a variable $row. You need a $row=Array(); before you can start treating $row as an array.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • This is not completely true. You can write `$row[] = 'X'` to add a value to the array and the array will be defined if it wasn't yet. However, if the query returns no rows, the `while` loop has no iterations, and `$row` won't be defined. In that case `$data = $row;` will fail. Your solution will solve that problem, and it's often a good idea to initialize variables like this, for clarity and for unexpected behaviour like this. – GolezTrol Feb 09 '15 at 18:13
  • @GolezTrol yes, it will be defined, but PHP will "notice" you about this. It is always recommended to declare all your variables. Recommended reading: http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – nikoskip Feb 09 '15 at 18:18
  • @nikoskip I don't think that's the case for the shorthand array notation. Which is supported by the notice occurring on line `$data = $row;` (= line 1257). See also, the chapter "Creating/modifying with square bracket syntax" on the [PHP documentation about arrays](http://php.net/manual/en/language.types.array.php). ("Creating" kinda gives it away). Nevertheless, I completely agree with you about initializing variables. – GolezTrol Feb 09 '15 at 18:40