[英]How to display results by creating variables?
我是PHP的新手,請原諒我的爛攤子。 對於像我這樣的人來說,這是一個非常復雜的查詢。
請參閱下面的查詢。 現在所有結果都顯示在$ row ['Post']中; 相反,我希望能夠做到這樣的事情:
$ somerow = $ row ['some_row']; $ somerow2 = $ row ['some_row2'];
然后能夠在上面使用的$變量的任何地方使用它。
我嘗試使用下面這個,但它不起作用:
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");
}
}
我怎樣才能實現這一目標?
完整查詢:
$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']);
}
}
您可以將每一行存儲在一個數組中,然后選擇要使用的行。
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
}
如果你想在任何地方使用這些變量,你需要將它們設置為全局變量,但是我建議不要使用全局變量來處理類似的事情或者對於任何事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.