Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 双mysql_fetch_数组(从另一个表获取信息以获取数据)/nest_Php_Mysql - Fatal编程技术网

Php 双mysql_fetch_数组(从另一个表获取信息以获取数据)/nest

Php 双mysql_fetch_数组(从另一个表获取信息以获取数据)/nest,php,mysql,Php,Mysql,我试图寻找这个问题,但他们的错误不同。出于某种原因,我使用PHP5.2 我试图做的是,从一个表中获取mysql\u fetch\u数组,然后使用该表的一列从另一个表中获取一行 这是我的代码高级抱歉可怕的格式 $sql = mysql_query("SELECT * FROM testimonies WHERE visibility != 'Hide' ORDER BY date_submitted DESC"); $testimonyCount = mysql_num_rows($sql); /

我试图寻找这个问题,但他们的错误不同。出于某种原因,我使用PHP5.2

我试图做的是,从一个表中获取mysql\u fetch\u数组,然后使用该表的一列从另一个表中获取一行

这是我的代码高级抱歉可怕的格式

$sql = mysql_query("SELECT * FROM testimonies WHERE visibility != 'Hide' ORDER BY date_submitted DESC");
$testimonyCount = mysql_num_rows($sql); // count the output amount
if ($testimonyCount > 0) {

//get data 'testimonies' table
while($info = mysql_fetch_array($sql)){ 
$username = $info['username'];

//Get id (in admin table) where username = username (in testimonies table)
$userid = mysql_query("SELECT * FROM admin WHERE username = $username LIMIT 1");
while($user = mysql_fetch_array($userid)){ $id = $user['id'];}

         $testimonies .= "<div class='row'><div class='col-lg-2'><center>
<img src='Pictures/Profile_Pictures/".$userid['id'].".jpg' width='160' height='160'>
<br>".$info['username']."</center></div><div class='col-lg-2'><center>
<img src='back/inventory_images/34.jpg' width='160' height='160'><br>"
.$info['product_used']."</center></div><div class='col-lg-8'><center><u>(Date: "
.$info['date_submitted']." - Ordered from our branch in <strong>"
.$info['branch_ordered']."</strong>, City)</u></center>".$info['testimony']."
</div></div><br>";
}
} 
else {
$testimonies = "No one has submitted their testimonies yet.";
}
其他的来自我搜索的,在第一个循环中获取他们的数据,但我什么都没有得到。第二个循环当然是个问题。救命啊


谢谢。

首先我要提到的是,以可以循环的方式构建查询是一个非常糟糕的主意。就像你的每个证词一样,你正在执行另一个查询来获取管理员信息。因此,对于10个证词,您将对MySQL进行10次访问[+1用于原始查询]。您通常希望进入,获取数据,然后离开

因此,更好的处理方法是让MySQL为您做这件事,加入一个连接:

选择* 从证言内部加入证言上的管理员。用户名=admin.username 能见度在哪里隐藏 按提交日期的订单说明

上面只会返回你的证词与匹配的管理员。如果你想返回证词,即使他们没有管理员,你也需要外部加入。您将用左外部联接管理替换内部联接管理

因此,请删除第二个while循环并尝试


此外,mysql_uu函数也不推荐使用。。所以你不应该用它们来开发新代码。试着让我的更改按原样工作,但是你应该考虑一下PDO[],

BTW,我正在使用PHP 5.2,因为我目前使用的免费Web主机只提供那个版本。谢谢你的快速回复。这似乎比我的主意好多了。那么,在这种情况下,我如何告诉mysql我正在从admin表中获取列,另外两列的名称相同:id和另外一列-类似于$info[admin.id']?您需要将列别名为-table1.id为IDT1,table2.id为IDT2等。然后您可以通过别名标题获取列。请,它们不再得到维护,而是在使用。而是学习,并使用或。这将帮助你做出决定。
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/_____/public_html/Testimonies.php on line xx
Notice: Undefined variable: id in /home/_____/public_html/Testimonies.php on line xx