2012-05-11 6 views
0

私は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']); 
} 
} 
+0

重複を避けるためにループを使用する方法を学びます。選択したクエリは番号以外は同じです。 – Lekensteyn

+0

また、これまでの標準的な答えは、[sql-injection](http://www.bobby-tables.com)に注意し、廃止予定のmysqlの使用を中止しますが、[mysqli](http: //php.net/mysqli)または[PDO](http://php.net/pdo)を参照してください。 – Zombaya

答えて

2

あなたは、アレイ内の各行を保存し、後で使用したい行を選ぶことができます。

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 
} 

あなたはどこでもそれらの変数を使用したい場合は、それらをグローバルにする必要がありますが、私はそのようなまたはそのことについては、一般的に何のためのもののためのグローバル変数を使用しないことをアドバイスします。

+0

ちょっとゾンバヤ。今これをチェックしてみましょう。ありがとう – ariel

+0

上記のクエリを使用すると、実際には2つのクエリを表示する必要がある場合に1つの結果しか表示されません。 – ariel

+0

あなたはあなたの質問に1つの質問を投稿したので、どこから2番目の質問を取得する必要がありますか? – Zombaya

関連する問題