Please, where is the problem?

Unable to jump to row 0 on MySQL result index 3 in c:\apache\htdocs\cd_shop\funkcie_s_databazou.php on line 1041


function zrataj_cenu($kosik)
//funkcia pocitajuca vyslednu sumu vsetkych poloziek v nakupnom kosiku
{
  $cena = 0.0;
  if(is_array($kosik))
  {
    $conn = db_connect();
    foreach($kosik as $EAN_kod => $qty)
    {  


      $query = "select cena from albumy where EAN_kod='$EAN_kod'";
      $result = mysql_query($query);
if (!$result["cena"])
        $query = "select cena from ciste_media where EAN_kod='$EAN_kod'";   

//$query = "select cena from albumy where EAN_kod='$EAN_kod'";
//$query1 = "select cena from ciste_media where EAN_kod='$EAN_kod'";

//$result=$query+$query1;

      $result = mysql_query($query);


//if (!$result = vyber_detaily_cd2($EAN_kod))
//      $result = vyber_detaily_media2($EAN_kod);



      if ($result)
      {
        $cena_polozky = mysql_result($result, 0, "cena");[B]  this is 1041[/B]
        $cena +=$cena_polozky*$qty;
      }
    }
  }
  return $cena;
}

Dani AI

Generated

Short answer: that PHP warning means you called mysql_result (or tried to read row 0) on a query that either failed or returned zero rows. In the snippets above the two root mistakes were treating the query resource like an array (e.g. if (!$result['cena'])) and re-running/overwriting the query result without first checking whether the first query returned any rows. was right to point out you must check the row count and only fetch when rows exist; fixed the immediate crash but still needs a robust fallback for missing products.

A cleaner, safer approach is to fetch the price with a single parameterized query and handle the "not found" case explicitly. Example using PDO (parameterized, avoids SQL injection, returns either table's cena or NULL if missing):

$sql = "SELECT COALESCE(
          (SELECT cena FROM albumy WHERE EAN_kod = ? LIMIT 1),
          (SELECT cena FROM ciste_media WHERE EAN_kod = ? LIMIT 1)
        ) AS cena";
$sth = $pdo->prepare($sql);
$sth->execute([$ean, $ean]);
$row = $sth->fetch(PDO::FETCH_ASSOC);

if ($row && $row['cena'] !== null) {
    $price = (float)$row['cena'];
    $total += $price * (int)$qty;
} else {
    // product missing: decide on default behavior (log, skip, or show error)
}

Quick troubleshooting and best practices: verify your DB connection and check PDO::errorInfo() (or mysql_error() if still using old API), validate that EAN_kod and qty are sane before querying, store money as DECIMAL(10,2) (or in cents as integers) and avoid floating-point rounding for currency (use BCMath or integer cents if exactness matters). Log any missing EANs so you can correct data. This addresses the original warning and gives a more reliable price lookup and fallback.

Recommended Answers

All 10 Replies

Just because the result is valid does not mean that there are rows.

It would be better to check if the result contains rows before attempting to jump to a particular one. For example

if(mysql_num_rows($result) >= 1){
$cena_polozky = mysql_result($result,0,"cena");
}

Please, where is the problem?

Unable to jump to row 0 on MySQL result index 3 in c:\apache\htdocs\cd_shop\funkcie_s_databazou.php on line 1041


function zrataj_cenu($kosik)
//funkcia pocitajuca vyslednu sumu vsetkych poloziek v nakupnom kosiku
{
  $cena = 0.0;
  if(is_array($kosik))
  {
    $conn = db_connect();
    foreach($kosik as $EAN_kod => $qty)
    {  


      $query = "select cena from albumy where EAN_kod='$EAN_kod'";
      $result = mysql_query($query);
if (!$result["cena"])
        $query = "select cena from ciste_media where EAN_kod='$EAN_kod'";   

//$query = "select cena from albumy where EAN_kod='$EAN_kod'";
//$query1 = "select cena from ciste_media where EAN_kod='$EAN_kod'";

//$result=$query+$query1;

      $result = mysql_query($query);


//if (!$result = vyber_detaily_cd2($EAN_kod))
//      $result = vyber_detaily_media2($EAN_kod);



      if ($result)
      {
        $cena_polozky = mysql_result($result, 0, "cena");[B]  this is 1041[/B]
        $cena +=$cena_polozky*$qty;
      }
    }
  }
  return $cena;
}

end quote.

thank you, this fix this mistake, but final price $cena still is uncorrectly.Is this select from database correct?

thank you, this fix this mistake, but final price $cena still is uncorrectly.Is this select from database correct?

Most likely your error comes from this point right here:

if (mysql_num_rows($result) >= 1)
{
$cena_polozky = mysql_result($result, 0, "cena");
$cena +=$cena_polozky*$qty;
}
}
}
return $cena;
}

What should your code do if there are no rows in the result in this case it is going to output $cena that has been unaltered. So what ever the value was before the return will remain.

I would suggest that you find a way to give a value to $cena when there are no rows returned from the database.

function zrataj_cenu($kosik)
//funkcia pocitajuca vyslednu sumu vsetkych poloziek v nakupnom kosiku
{
  $cena = 0.0;
  if(is_array($kosik))
  {
    $conn = db_connect();
    foreach($kosik as $EAN_kod => $qty)
    {  


      $query = "select cena from albumy where EAN_kod='$EAN_kod'";
      $result = mysql_query($query);




      if ($result)
      {

         $cena_polozky = mysql_result($result,0,"cena");

        $cena +=$cena_polozky*$qty;
      }
    }
  }
  return $cena;
}

Just now is good.Because I find cena from one table albumy. Bud if I would select and find another cena from table ciste_media this is problem. How I do this? Please change my code.

Where do you do a query on table ciste_media? I don't see that in your code. All I see is from the albumy table and as I stated before you need to fix the check for rows in your result from the original query.

Are you trying to say that you want to have it where if the albumy returns zero rows that you should check another table?

If so you need to add an else statement to the check for number of rows returned.

this is code, where I do select from table ciste_media. If I do this, final price in shopping basket $cena is 0. I thing that is problem with selection from database.

function zrataj_cenu($kosik)
//funkcia pocitajuca vyslednu sumu vsetkych poloziek v nakupnom kosiku
{
  $cena = 0.0;
  if(is_array($kosik))
  {
    $conn = db_connect();
    foreach($kosik as $EAN_kod => $qty)
    {  


      $query = "select cena from albumy where EAN_kod='$EAN_kod'";
      $result = mysql_query($query);
//      $record = mysql_fetch_array($query);
if (!$result['cena'])
        $query = "select cena from ciste_media where EAN_kod='$EAN_kod'";   

//$query = "select cena from albumy where EAN_kod='$EAN_kod'";
//$query1 = "select cena from ciste_media where EAN_kod='$EAN_kod'";

//$result=$query+$query1;

      $result = mysql_query($query);
//$record = mysql_fetch_array($query);

//if (!$result = vyber_detaily_cd2($EAN_kod))
//      $result = vyber_detaily_media2($EAN_kod);



      if ($result)
      {
      if(mysql_num_rows($result) >= 1){
         $cena_polozky = mysql_result($result,0,"cena");
         }

        $cena +=$cena_polozky*$qty;
      }
    }
  }
  return $cena;
}

Yep you don't have a proper condition and so overwriting your value.
Try something like this:

$qry = "select cena from albumy where EAN_kod='$EAN_kod'";
$result = mysql_query($qry);
if(mysql_num_rows($result) < 1){
     $qry = "select cena from ciste_media where EAN_kod='$EAN_kod'";
     $result = mysql_query($qry);
}

What this will do is check if there is anything in table albumy that matches the EAN_kod. If it returns 0 rows it will check the ciste_media table. In both cases it will then go to the next check where if there are results then it takes the first one and adds it with the quantity to the cena of the overall shopping cart.

I think that should get you on the right track.

Bombaaaaaaaaaaa

You are really boss. Thank you very very very very much.

Don't forget to mark this solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.