0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

This must be something simple...

This script calls a db_prcislo() function.

<?php 
  require_once "kniznica/vyk/uzivatel_vyk.php"; db_prcislo();
  echo '<select name="problem_cislo">';
  while($row = mysql_fetch_array($vysledok)){
   echo "<option value='".$row['problem_cislo']."'>".$row['problem_cislo']."</option>";} 
  echo "</select>";
?>

This is the db_prcislo() function

function db_prcislo() {
$poziadavka = "SELECT problem_cislo FROM problem_cislo";
$vysledok = mysql_query($poziadavka);
return $vysledok;}

Yet, I am getting undefined variable.

Community
  • 1
  • 1
  • Notice: Undefined variable: vysledok in C:\wamp\www\intranet_pre_ikeacmpts\pridatartikel.php on line 24 – PatrikVrbovsky Aug 15 '12 at 14:50
  • 2
    In the first block, you are not assigning any value to `$vysledok` before using it in `mysql_fetch_array`. In addition to that, please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – DCoder Aug 15 '12 at 14:51
  • you are not storing the value returned by the function,try this $vysledok=db_prcislo(); – Tarun Aug 15 '12 at 14:52

3 Answers3

0

The error is occurring because you're trying to use the variable $vysledok in your while() loop, but you never assigned it. You only assigned it in your db_prcislo() function.

Update to this and it should work:

<?php 
  require_once "kniznica/vyk/uzivatel_vyk.php";

  $vysledok = db_prcislo();
  echo '<select name="problem_cislo">';
  while($row = mysql_fetch_array($vysledok)){
   echo "<option value='".$row['problem_cislo']."'>".$row['problem_cislo']."</option>";} 
  echo "</select>";
?>
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

You're not doing anything with the return value from db_prcislo.

Instead of

db_prcislo();

you should have

$vysledok = db_prcislo();
awm
  • 6,526
  • 25
  • 24
0

You must retrieve the result of your function call:

Replace:

db_prcislo();

With:

$vysledok = db_prcislo();
Jocelyn
  • 11,209
  • 10
  • 43
  • 60