0

My original query was to select data from phpadmin table and display into table.

$sql="select * from table";
$result=mysql_query($sql) or die(mysql_error());

while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
{
    extract($myrow);
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[0]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[1]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[2]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[3]."</td>";    
}


Then I need to display another column using value of $myrow[0] to execute another query to get the value.

$sql="select * from table";
$result=mysql_query($sql) or die(mysql_error());

while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
{
    extract($myrow);

    $id = $myrow[0];
    $sql="select amount from table2 where id like '%$id%'";
    $result = mysqli_query($conn, $sql);

    echo "<td align='left' bgcolor='$bgclr'>".$myrow[0]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[1]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[2]."</td>";    
    echo "<td align='left' bgcolor='$bgclr'>".$myrow[3]."</td>";    

    while ($row=mysqli_fetch_row($result)) {

            echo "<td align='left' bgcolor='$bgclr'>".$row[0]."</td>";echo "</tr>";
        }   
    }


And my page became blank. And the error was at

$result = mysqli_query($conn, $sql);

Is it the correct method or how should I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
hatched
  • 795
  • 2
  • 9
  • 34
  • 1
    Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Jens Jul 06 '18 at 06:15
  • 1
    usually you're better off using a join statement, so that you wouldn't need to repeat the query every iteration – Kevin Jul 06 '18 at 06:15
  • you mix mysql and mysqli – Jens Jul 06 '18 at 06:16
  • `like` with id makes no sence – Jens Jul 06 '18 at 06:16
  • 1
    Use prepared Statements to prevent SQL injection – Jens Jul 06 '18 at 06:16
  • " phpadmin table" Phpmyadmin is not a database, its a php script, just like yours –  Jul 06 '18 at 06:17
  • Always post the error message if you get an error – Jens Jul 06 '18 at 06:17
  • 1
    It would be much better if you combined both these SQL statements into 1 query. – Nigel Ren Jul 06 '18 at 06:21
  • I'm not able to combine queries because I'm selecting data from different tables with different connections. – hatched Jul 06 '18 at 06:23
  • Running iterated queries based on an earlier query's result set is evidence of poor database interaction. You and all researchers should ignore the accepted answer and perform a single query using a JOIN of the two tables. If your question was clearer, I could write the appropriate query. – mickmackusa Jun 22 '19 at 12:27

1 Answers1

3

You overwrite variable $result while executing query inside the loop here:

$sql="select amount from table2 where id like '%$id%'";
$result = mysqli_query($conn, $sql);

Change variable name $result inside the loop and code will be like:

$sql="select amount from table2 where id like '%$id%'";
$result2 = mysqli_query($conn, $sql);

Also update it here:

while ($row=mysqli_fetch_row($result2)) {

            echo "<td align='left' bgcolor='$bgclr'>".$row[0]."</td>";echo "</tr>";
        }   
    }
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36