首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过创建变量来显示结果?

如何通过创建变量来显示结果?
EN

Stack Overflow用户
提问于 2012-05-12 05:05:39
回答 1查看 61关注 0票数 0

我是PHP新手,所以请原谅我的混乱。对于像我这样的人来说,这是一个非常复杂的查询。

请参阅下面的查询。现在,所有结果都显示在$row‘’Post‘中;相反,我希望能够执行以下操作:

row=$row‘’some_row‘;$somerow2 =$row’‘some_row 2’;

然后可以通过上面使用的$variables在任何地方使用它。

我试着在下面使用它,但它不起作用:

代码语言:javascript
复制
if($rows == 0)
{
print("");

}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{

$postid = htmlspecialchars($row['post_id']);
$postname = htmlspecialchars($row['post_name']);


print("$postname and the id is $postid");
}

}

我如何才能做到这一点?

完整查询:

代码语言:javascript
复制
$denied = "Denied";
$userid = Drawn from db for user viewing;

$sql = "SELECT  concat(  (select client_name from user_accounts where 
User_id = tv.User_id), ' commanded ' , title_1  ,' on ', CAST(other_date AS 
CHAR(10)) ) AS Post FROM client_visits tv where User_id in (select contact_id 
from contacts where User_id = '$userid' ) and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_2  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM client_notes tv where User_id in (select contact_id from 
contacts where User_id = '$userid' ) and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_3  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM client_media tv where User_id in (select contact_id 
from contacts where User_id = '$userid' ) and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_4  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM client_stats tv where User_id in (select contact_id from 
contacts where User_id = '$userid' ) and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_5 ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM client_current_list  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_6 ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM client_past  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_7  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM  client_listers  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_8  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM    client_events  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_9  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM    client_admissions  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'
union
SELECT  concat(  (select client_name from user_accounts where User_id 
= tv.User_id), ' commanded ' , title_10  ,' on ', CAST(other_date AS CHAR(10)) )
AS Post FROM    client_immu  tv
where User_id in (select contact_id from contacts where User_id = '$userid' )
and user_allowed = '$denied'
or User_id in (select User_id from contacts where contact_id = '$userid')
and user_allowed = '$denied'";

$query = mysql_query($sql) or die ("Error: ".mysql_error());

$result = mysql_query($sql);

if ($result == "")
{
echo "";
}
echo "";


$rows = mysql_num_rows($result);

if($rows == 0)
{
print("");

}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{

print($row['Post']);
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-12 05:56:39

您可以将每一行存储在一个数组中,然后挑选出您想要使用的行。

代码语言:javascript
复制
if($rows == 0)
{
    print("I have no rows!");
}
else
{
   $allrows = array();
   while($row = mysql_fetch_array($query))
   {
       $allrows[] = $row;
   }
   $row1 = $allrows[0];
   $row2 = $allrows[1];
   echo $row1['Post']; // Print out first row
   echo $row2['Post']; // Print out second row
   var_dump($allrows); // Print out all the rows and the structure of this array
}

如果你想在任何地方都使用这些变量,你需要将它们设置为全局变量,但我建议不要将全局变量用于类似的事情或一般情况下的任何事情。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10558623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档