Problem with an error

Greetings to all the forum!

I have a problem with an error that occurs after testing localhost:
Notice: Undefined offset: 2 in C:
Notice: Undefined offset: 1 in C:
follows the script:

<?php
                        $result = pdo_query("select * from vesti where izdvajamo = 1 order by id_vesti desc limit 1");
                        $vest = pdo_fetch_array($result);
                        $array = explode("-", (string)$vest["datum"]);
                        $date = $array[2] . "";
                        switch ($array[1])
                        {
                            case "1": $date .= "januar"; break;
                            case "2": $date .= "februar"; break;
                            case "3": $date .= "mart"; break;
                            case "4": $date .= "april"; break;
                            case "5": $date .= "maj"; break;
                            case "6": $date .= "jun"; break;
                            case "7": $date .= "jul"; break;
                            case "8": $date .= "avgust"; break;
                            case "9": $date .= "septembar"; break;
                            case "10": $date .= "oktobar"; break;
                            case "11": $date .= "novembar"; break;
                            case "12": $date .= "decembar"; break;
                        }
                        $date .= " " . $array[0];
                        echo'
                        <p class="leftDate">
                            <span style="font-weight: bold;">Objavljeno</span>
                            '.$date.'
                        </p>
                        <p class="leftSponsoredFirst">
                            <a href="vesti.php?id='.$vest["id_vesti"].'" class="moar">'.$vest["naslov"].'</a>
                        </p>
                        <fieldset class="hr">
                            <legend class="hRight">
                                <a href="vesti.php?id='.$vest["id_vesti"].'" class="leftMore">Saznajte više</a>
                            </legend>
                        </fieldset>';
                        ?>

Advance thanks to assistance

I would do

echo "<pre>" . print_r($array, 1) . "</pre>";

to see you’re getting the array you expect, plus I would change the name from $array to $myArray (or something else than array) just to be safe.

also, don’t fetch everything when the only column you want is “datum”. and adding an empty string to a string is useless.

note, for this case you could also use the fetchColumn() method, sou you wouldn’t have to explicitly address the column.

The error messages suggest that the array could not be created. This could be because the query didn’t return any data, or that the data could not be exploded into the array.

You need to check that your query returns something (as you only want one row back, perhaps check the row count and handle it gracefully if it isn’t 1), and then you need to check that your array has the correct number of elements after you run explode() to create it. Again, if it hasn’t been able to split the string for some reason, handle it gracefully. Handling those errors would also tell you which is causing the problem, the query or the content of $vest[‘datum’].

There are more columns used in the code lower down. But still maybe only a small fraction of those being returned of course.

Thank you very much everyone for your answers, problem solved

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.