Php 查询结果到变量
我有五张桌子。我需要从一个表中选择最近的4篇文章,并在四个单独的变量中获取这些文章的ID,以用于其他表的查询:Php 查询结果到变量,php,mysql,Php,Mysql,我有五张桌子。我需要从一个表中选择最近的4篇文章,并在四个单独的变量中获取这些文章的ID,以用于其他表的查询: SELECT user FROM table1 WHERE subcategory = "$input" ORDER BY id DESC LIMIT 4; 所以我有我需要的。现在,我如何将这四个userid结果分配给新变量,以便在同一页面上的未来查询中使用 我需要像这样存储它: $user1 = "Sue"; $user2 = "Dave"; $user3 = "Alicia";
SELECT user FROM table1 WHERE subcategory = "$input" ORDER BY id DESC LIMIT 4;
所以我有我需要的。现在,我如何将这四个userid结果分配给新变量,以便在同一页面上的未来查询中使用
我需要像这样存储它:
$user1 = "Sue";
$user2 = "Dave";
$user3 = "Alicia";
$user4 = "Tim";
SELECT * FROM table2 WHERE user = $user1;
SELECT * FROM table2 WHERE user = $user2;
SELECT * FROM table2 WHERE user = $user3;
SELECT * FROM table2 WHERE user = $user4;
Sue Dave Alicia Tim
Sue Profile Dave Profile Alicia Profile Tim Profile
>>Answers to questions about work:
Sue Income Dave Income Alicia Income Tim Income
Sue Years Dave Years Alicia Years Tim Years
>>Answers to questions about school:
Sue Degree Dave Degree Alicia Degree Tim Degree
Sue Schools Dave Schools Alicia Schools Tim Schools
>>Answers to questions about life:
Sue Born Dave Born Alicia Born Tim Born
Sue Parents Dave Parents Alicia Parents Tim Parents
$id=1;
${"user".$id}="Sue";
$id=1;
$user[$id]="Sue";
所以我可以这样使用它:
$user1 = "Sue";
$user2 = "Dave";
$user3 = "Alicia";
$user4 = "Tim";
SELECT * FROM table2 WHERE user = $user1;
SELECT * FROM table2 WHERE user = $user2;
SELECT * FROM table2 WHERE user = $user3;
SELECT * FROM table2 WHERE user = $user4;
Sue Dave Alicia Tim
Sue Profile Dave Profile Alicia Profile Tim Profile
>>Answers to questions about work:
Sue Income Dave Income Alicia Income Tim Income
Sue Years Dave Years Alicia Years Tim Years
>>Answers to questions about school:
Sue Degree Dave Degree Alicia Degree Tim Degree
Sue Schools Dave Schools Alicia Schools Tim Schools
>>Answers to questions about life:
Sue Born Dave Born Alicia Born Tim Born
Sue Parents Dave Parents Alicia Parents Tim Parents
$id=1;
${"user".$id}="Sue";
$id=1;
$user[$id]="Sue";
不要混淆问题,但:最终目标是拥有多行,但只有四列,其中行中的所有数据由用户在最上面一行排序。注意这一点很重要,因为每一组行都由下面数据中所包含内容的描述分隔开。这些只是简单的直柱。大概是这样的:
$user1 = "Sue";
$user2 = "Dave";
$user3 = "Alicia";
$user4 = "Tim";
SELECT * FROM table2 WHERE user = $user1;
SELECT * FROM table2 WHERE user = $user2;
SELECT * FROM table2 WHERE user = $user3;
SELECT * FROM table2 WHERE user = $user4;
Sue Dave Alicia Tim
Sue Profile Dave Profile Alicia Profile Tim Profile
>>Answers to questions about work:
Sue Income Dave Income Alicia Income Tim Income
Sue Years Dave Years Alicia Years Tim Years
>>Answers to questions about school:
Sue Degree Dave Degree Alicia Degree Tim Degree
Sue Schools Dave Schools Alicia Schools Tim Schools
>>Answers to questions about life:
Sue Born Dave Born Alicia Born Tim Born
Sue Parents Dave Parents Alicia Parents Tim Parents
$id=1;
${"user".$id}="Sue";
$id=1;
$user[$id]="Sue";
如果我可以将id结果读入单独的变量,那么对于每个部分(问题),我可以执行以下操作:
SELECT * FROM income WHERE user = "Sue";
SELECT * FROM income WHERE user = "Dave";
SELECT * FROM income WHERE user = "Alicia";
SELECT * FROM income WHERE user = "Tim";
SELECT * FROM degree WHERE user = "Sue";
SELECT * FROM degree WHERE user = "Dave";
SELECT * FROM degree WHERE user = "Alicia";
SELECT * FROM degree WHERE user = "Tim";
最好使用连接,然后运行多个查询。下面是代码
SELECT t1.user, t2.* FROM table1 as t1 LEFT JOIN table2 as t2 ON t1.id = t2.user_id WHERE t1.subcategory = "$input" ORDER BY t1.id DESC LIMIT 4
我希望您可以以t1.id=t2的速度更改两个表中的相同键。用户id而您所要求的可以像这样实现:
$user1 = "Sue";
$user2 = "Dave";
$user3 = "Alicia";
$user4 = "Tim";
SELECT * FROM table2 WHERE user = $user1;
SELECT * FROM table2 WHERE user = $user2;
SELECT * FROM table2 WHERE user = $user3;
SELECT * FROM table2 WHERE user = $user4;
Sue Dave Alicia Tim
Sue Profile Dave Profile Alicia Profile Tim Profile
>>Answers to questions about work:
Sue Income Dave Income Alicia Income Tim Income
Sue Years Dave Years Alicia Years Tim Years
>>Answers to questions about school:
Sue Degree Dave Degree Alicia Degree Tim Degree
Sue Schools Dave Schools Alicia Schools Tim Schools
>>Answers to questions about life:
Sue Born Dave Born Alicia Born Tim Born
Sue Parents Dave Parents Alicia Parents Tim Parents
$id=1;
${"user".$id}="Sue";
$id=1;
$user[$id]="Sue";
因此$user1将具有“Sue”,因此可以很容易地将数组用于此目的,如下所示:
$user1 = "Sue";
$user2 = "Dave";
$user3 = "Alicia";
$user4 = "Tim";
SELECT * FROM table2 WHERE user = $user1;
SELECT * FROM table2 WHERE user = $user2;
SELECT * FROM table2 WHERE user = $user3;
SELECT * FROM table2 WHERE user = $user4;
Sue Dave Alicia Tim
Sue Profile Dave Profile Alicia Profile Tim Profile
>>Answers to questions about work:
Sue Income Dave Income Alicia Income Tim Income
Sue Years Dave Years Alicia Years Tim Years
>>Answers to questions about school:
Sue Degree Dave Degree Alicia Degree Tim Degree
Sue Schools Dave Schools Alicia Schools Tim Schools
>>Answers to questions about life:
Sue Born Dave Born Alicia Born Tim Born
Sue Parents Dave Parents Alicia Parents Tim Parents
$id=1;
${"user".$id}="Sue";
$id=1;
$user[$id]="Sue";
或者更好的方法是,尝试在查询中使用Join一次获取所有数据,然后根据需要重新格式化数据。如果不想使用Join,那么在执行第一个查询后,应该使用foreach循环
$user=array(); // i would suggest you for making an array
foreach($result_array as $key->$value)
{
//here you will get the ids in $value['user']
// and then you can use them in your queries
// so no need to store them in different variables and remembering them.
Edited:
$user[$key] = $value['user'];
}
我不明白这是如何让我绕过分隔符的(上面以>>答案开头的行),我已经有了一个连接,我可以在列中打印输出,但不能使用分隔符。这就是为什么我认为我需要第一个查询将用户作为四个不同的变量提供给我。直到我在表1 LIMIT 4上运行查询并获得四个最近活动的用户,我才知道这是Sue。如何从数据库中获取这些用户并为这4个用户创建用户变量?不知怎的,我错过了它。我现在加入了:
SELECT videos.proid,videos.code,videos.views,pros.id,videos.qid,questions.question,pros.fname,pros.lname从视频内部加入pros.id=videos.proid在视频内部加入questions.qid=questions.id其中subcatid='$subcatid'限制4
获取用户名,他们的简介,以及第一个问题的答案。我被困在获取剩余的问题,并像我原来的帖子/问题中的例子一样将其分解。这就是为什么我认为我需要多个查询?如果你不熟悉查询并且想保持简单,你可以像anuj arora的回答那样循环,或者你必须使用像while($row=mysql\u fetch\u array($res))这样的循环来循环每个结果,并在循环中执行select*from。。。user=“$user1”
我明白了,但是我如何将“Sue”分配给$user1,“Dave”分配给$user2,“Alicia”分配给$user3,“Tim”分配给$user4,这样我就可以在以后的脚本中需要时调用$user1-$user4了?所以我有了这个:
并且它输出这个:数组(5){[0]=>数组(1){“proid”=>string(2)”11}[1]=>array(1){[“proid”]=>string(2)“15”}[2]=>array(1){[“proid”]=>string(2)“11”}[3]=>array(1){[“proid”]=>string(2)“15”}[4]=>bool(false)
我不知道如何从该数组中获取proid,以便在下一次查询中使用。为什么要将整个数组从mysql\u fetch\u assoc($result)分配到$arr[]。你只需要得到“proid”。因此,我再次建议您使用foreach循环,它将满足您的需求。相信我,再试一次。