The query is return an empty value on the frist row which means that the supposed value in the first row is in the second row , etc etc . How do i correct this?

    $query1 = "SELECT * FROM opentrades"; //You don't need a ; like you do in SQL
    $result1 = mysql_query($query1);
    echo "<table border = '1px'>"; // start a table tag in the HTML
    while($row1 = mysql_fetch_array($result1)){  
    echo "<tr><td>" . "Selection" . "</td><td>" . "Profit/Loss" . "</td></tr>";
    echo "<tr><td>" . $row1['selection']."<td>".$offerpricepl. "</tr>";
    if ($row1['selection']=='eur/usd')
                {
                $bidpricepl=$bid;


            $offerpricepl=$bid1;

            }
    elseif ($row1['selection']=='usd/jpy')
            {
            $bidpricepl=$bid2;

            $offerpricepl=$bid3;

            }
    elseif ($row1['selection']=='usd/cad')
            {
            $bidpricepl=$bid4;

            $offerpricepl=$bid5;

            }
    elseif ($row1['selection']=='eur/jpy')
            {
            $bidpricepl=$bid6;

            $offerpricepl=$bid7;

            }
    elseif ($row1['selection']=='eur/chf')
            {
            $bidpricepl=$bid8;

            $offerpricepl=$bid9;

            }
    elseif ($row1['selection']=='gbp/usd')
            {
            $bidpricepl=$bid10;

            $offerpricepl=$bid11;

            }
    elseif ($row1['selection']=='aud/usd')
            {
            $bidpricepl=$bid12;

            $offerpricepl=$bid13;

            }
    elseif ($row1['selection']=='usd/chf')
            {
            $bidpricepl=$bid14;

            $offerpricepl=$bid15;

            }
}
echo "</table><br>";

Dani AI

Generated

Root cause (concise): the Profit/Loss cell is empty on the first row because the value used for that cell is assigned after the row is printed, so every printed PL comes from the previous loop iteration. The table header is also being emitted inside the loop and the row HTML is malformed, which makes the output harder to read and debug. spotted the order-of-operations bug, suggested moving the header out of the loop, and correctly noted the old mysql functions and the possibility of an empty DB row.

Practical fixes (apply in this order):

  • Emit the header once before entering the loop.
  • Compute the PL/offer value before echoing the row (avoid using a value that will only be set later in the same iteration).
  • Use a simple map to replace long if/elseif chains and reduce mistakes.
  • Ensure each table cell is closed properly so HTML rendering doesn't shift cells.

Example (illustrative; uses mysqli and a mapping approach rather than repeating the original if/elseif):

$map = ['eur/usd'=>$bid1, 'usd/jpy'=>$bid3 /* ... */];

echo "<table border='1'><tr><td>Selection</td><td>Profit/Loss</td></tr>";

while ($row = mysqli_fetch_assoc($result)) {
  $sel = strtolower(trim($row['selection']));
  $offer = isset($map[$sel]) ? $map[$sel] : '';
  echo "<tr><td>{$row['selection']}</td><td>{$offer}</td></tr>";
}

echo "</table>";

Debug checklist and cautions:

  • Turn on error reporting and log values (e.g., error_reporting(E_ALL); ini_set('display_errors',1);) and inspect var_dump($row) to confirm column contents and whitespace. Trim and normalize selection before lookup. Check for empty rows with SELECT COUNT(*) or LIMIT 1 to rule out a blank DB row. Lastly, migrate from the deprecated mysql extension to mysqli or PDO with prepared statements to avoid future incompatibilities (PHP 7+ removed the old mysql_* functions).

Recommended Answers

All 4 Replies

I don't know what happens, maybe you have an empty row in your database. Your code seem equal to Click Here
On option in your code that you lack an else-clause so if the row does not fit in the else-ifs, no value will be put in the cell.
In this link you can also read that fetch_array is depreciated from php 5.5.0. May you can solve it by using the MySQLi variant, see Click Here.

But where is the problem???

The problem is that $offerpricepl is not defined yet in the first iteration of the while loop. Put the test first and then echo the row:

...
while($row1 = mysql_fetch_array($result1)){  

    // this is the heading row
    echo "<tr><td>" . "Selection" . "</td><td>" . "Profit/Loss" . "</td></tr>";

    // now define the value of the $offerpricepl variable
    if ($row1['selection']=='eur/usd')
                {
                $bidpricepl=$bid;


            $offerpricepl=$bid1;

            }
    elseif ($row1['selection']=='usd/jpy')
            {
            $bidpricepl=$bid2;

            $offerpricepl=$bid3;

            }
    elseif ($row1['selection']=='usd/cad')
            {
            $bidpricepl=$bid4;

            $offerpricepl=$bid5;

            }
    elseif ($row1['selection']=='eur/jpy')
            {
            $bidpricepl=$bid6;

            $offerpricepl=$bid7;

            }
    elseif ($row1['selection']=='eur/chf')
            {
            $bidpricepl=$bid8;

            $offerpricepl=$bid9;

            }
    elseif ($row1['selection']=='gbp/usd')
            {
            $bidpricepl=$bid10;

            $offerpricepl=$bid11;

            }
    elseif ($row1['selection']=='aud/usd')
            {
            $bidpricepl=$bid12;

            $offerpricepl=$bid13;

            }
    elseif ($row1['selection']=='usd/chf')
            {
            $bidpricepl=$bid14;

            $offerpricepl=$bid15;

            }

    // now you can use the $offerpricepl variable
    echo "<tr><td>" . $row1['selection']."<td>".$offerpricepl. "</tr>";
}
echo "</table><br>";

The code would be also cleaner if you used switch instead of if / elseif.

Also I do not know why heading row must be repeated each iteration.

commented: this solves the question I think +3

I couldn't get the value of $offerpricepl

echo "<tr><td>" . $row1['selection']."<td>".$offerpricepl. "</tr>"; // There is missing the value of $offerpricepl variable and it will not define anywhere..

I think It should be $offerprice1=$row1['fsomefieldname']; then we can get the iterate value.

One more suggestion I would like to add here:

echo "<tr><td>" . "Selection" . "</td><td>" . "Profit/Loss" . "</td></tr>";  //That should be out of while loop so that heading will not repeat and for structured output.
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.