gpt4 book ai didi

php - 在另一个 mysql 查询 while 循环中调用 mysql 查询

转载 作者:行者123 更新时间:2023-11-29 22:05:32 27 4
gpt4 key购买 nike

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

代码应该做什么:

代码应该从机构表中获取数据,然后$row['name']应该用作下一个mysql查询的表,来自该$row['name'] $row['content'] 应该被抓取

但它没有按预期工作

最佳答案

您在任何地方都使用相同的变量。将它们更改为

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

编辑:

您的查询也是错误的

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

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

关于php - 在另一个 mysql 查询 while 循环中调用 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com