简体   繁体   中英

Calling a mysql query inside another mysql query while loop

include "mysql.php";
          $query= "SELECT ID,name,displayname,established,summary,searchlink,imagename,image FROM institutions ORDER BY rand() ";
          $result=mysql_query($query,$db);
          while($row=mysql_fetch_array($result))
            {
              echo "<div class='grid-item item1' style='background: ".ran_col().";'>"; 

                $query1= "SELECT content FROM ".$row['name']." limit 1";
                $result1=mysql_query($query1,$db);
                while($row1=mysql_fetch_array($result1))
                {
                            echo "<div class='content-short' data-id=".$row['name'].">";
                                $string = $row1['content'];
                                if (strlen($string) > 200) 
                                {
                                $trimstring = substr($string, 0,200). '...';
                                } 
                                else 
                                {
                                $trimstring = substr($string,0). '...';
                                }
                                echo $trimstring;

                            echo "</div>";
                }
              echo <"/div">;
            }

What code should do:

The code should grab data from institution table and after that $row['name'] should be used as table for next mysql query where from that $row['name'] table $row['content'] should be grabbed

but it is not working as expeted

You are using same variable everywhere. Change them to

include "mysql.php";
          $query= "SELECT ID,name,displayname,established,summary,searchlink,imagename,image FROM institutions ORDER BY rand() ";
          $result=mysql_query($query,$db);
          while($row=mysql_fetch_array($result))
            {
              echo "<div class='grid-item item1' style='background: ".ran_col().";'>"; 

                $query1= "SELECT content FROM ".$row['name']." limit 1";
                $result1=mysql_query($query1,$db);
                while($row1=mysql_fetch_array($result1))
                {
                            echo "<div class='content-short' data-id=".$row['name'].">";
                                $string = $row1['content'];
                                if (strlen($string) > 200) 
                                {
                                $trimstring = substr($string, 0,200). '...';
                                } 
                                else 
                                {
                                $trimstring = substr($string,0). '...';
                                }
                                echo $trimstring;

                            echo "</div>";
                }
              echo <"/div">;
            }

EDIT:

Also your query is wrong

$query1= "SELECT content FROM ".$row['name']" limit 1";

to

$query1= "SELECT content FROM ".$row['name']." limit 1";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM