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;
}

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.