Home php Throws an error Notice: Undefined variable:

Throws an error Notice: Undefined variable:

Author

Date

Category

Throws an error:

Notice: Undefined variable: dic in
/home/v/vladle43/muzikalka-onlayn.rf/public_html/all/dictant/class.php
on line 12

Tell me how to fix it


Answer 1, authority 100%

Since the question is only about notice , the point here is that you are trying to add elements to an array that does not exist yet:

$ dic [] = $ dictants_arr ['audio'];

Yes, PHP allows you to create variables on the fly without prior declaration, but in this case you do not create a new variable, but try to access it [] , which naturally leads to the note.

To solve this, in this case, it is enough to declare your array before the loop:

$ dic = array ();
while ($ dictants_arr = mysqli_fetch_assoc ($ dictant_from_bd)) {
  $ dic [] = $ dictants_arr ['audio'];
}

Answer 2, authority 50%

Insert the line $ dic = Array (); after $ class = .

Also, it looks like your query returns 0 lines … take a look.


Answer 3, authority 25%

session_start ();
include ("url"); // Here I connect to the database
// avoid a notice error if no GET is passed and immediately escaping the data
$ class = isset ($ _ GET ['class'])
  ? $ mysqli- & gt; real_escape_string ($ _ GET ['class'])
  : '';
$ query = "SELECT` audio` FROM `dictations` WHERE class = '$ class'";
// throw an error if the request does not work
if (! $ result = $ mysqli- & gt; query ($ query)) die ($ mysqli- & gt; error);
if ($ result- & gt; num_rows == 0) echo 'There are no entries'; // in case there is nothing in the database
else {
$ while ($ row = $ result_fetch_assoc ()) {
    echo $ row ['audio'];
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions