2011-04-06 58 views
2

我使用下面的代碼的MySQL查詢到時間/日期排序:如何將數組值添加到MySQL查詢?

mysql_select_db("user_live_now", $con); 

$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC"); 

while($row = mysql_fetch_array($result)) 
    { 
     print($row['user']); 
    } 

而不是讓PHP運行通過,並顯示在表中的所有值,我可以把它從一個數組顯示值?

+1

您的意思是隻顯示特定用戶? – Jon 2011-04-06 00:11:07

+0

是的!那正是我需要的。 – 2011-04-06 00:12:08

+0

然後以這樣的方式構建SQL查詢,只返回您感興趣的用戶。查找與'SELECT ... WHERE user IN('foo','bar')'類似的語法。 – Jon 2011-04-06 00:13:49

回答

1

那麼,你想在SQL查詢中找到特定的用戶來返回?編程方式構建查詢:

$users = array('User1','John','Pete Allport','etc'); 

$sql = "SELECT * FROM `users_newest_post` WHERE "; 
$i = 1; 
foreach($users as $user) 
{ 
    $sql .= "`username` = '$user'"; 
    if($i != count($users)) 
    { 
     $sql .= " OR "; 
    } 
    $i++; 
} 
$sql .= " ORDER BY `users_date_post` DESC"; 
$result = mysql_query($sql); 

這將讓你像一個查詢:

SELECT * FROM `users_newest_post` 
WHERE `username` = 'User1' 
OR `username` = 'John' 
OR `username` = 'Pete Allport' 
OR `username` = 'etc' 
ORDER BY `users_date_post` 
DESC 

所以,你要找到某個特定日期或兩個日期之間的所有帖子,有點難以做到這一點不知道表結構,但你有這樣的事情做到這一點:

//Here's how to find all posts for a single date for all users 
$date = date('Y-m-d',$timestamp); 
//You'd pull the timestamp/date in from a form on another page or where ever 
//Like a calendar with links on the days which have posts and pass the day 
//selected through $_GET like page.php?date=1302115769 
//timestamps are in UNIX timestamp format, such as you'd get from time() or strtotime() 
//Note that, without a timestamp parameter passed to date() it uses the current time() instead 

$sql = "SELECT * FROM `posts` WHERE `users_date_post` = '$date'" 
$results = mysql_query($sql); 
while($row = mysql_fetch_assoc($results)) 
{ 
    echo $row['post_name'] . $row['users_date_post']; //output something from the posts 
} 

//Here's how to find all posts for a range of dates 
$startdate = date('Y-m-d',$starttimestamp); 
$enddate = date('Y-m-d',$endtimestamp); 
//Yet again, date ranges need to be pulled in from somewhere, like $_GET or a POSTed form. 
//Can also just pull in a formatted date rather than a timestamp and use it straight up instead, rather than going through date() 
$sql = "SELECT * FROM `posts` WHERE `users_date_post` BETWEEN '$startdate' AND '$enddate'"; 

//could also do: 
//"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$endate'" 

$results = mysql_query($sql); 
while($row = mysql_fetch_assoc($results)) 
{ 
    //output data 
} 

要查找特定用戶的帖子,你會修改聲明是這樣的:

$userid = 5; //Pulled in from form or $_GET or whatever 
"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$enddate' AND `userid` = $userid" 
+0

如果我需要所有這些值,該怎麼辦? – 2011-04-06 01:02:33

+0

你是什麼意思?哪些值? – Phoenix 2011-04-06 01:07:49

+0

所有的人:約翰,皮特奧爾波特等,我試圖從所有用戶的表中拉出用戶的朋友,如果這是有道理的? – 2011-04-06 01:10:11

0

要轉儲結果到一個數組執行以下操作:

mysql_select_db("user_live_now", $con); 

$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC"); 

while($row=mysql_fetch_assoc($result)) 
{ 
    $newarray[]=$row 
} 
+0

不,不,這很好,除非我需要能夠將數組加載到查詢中,如果有意義的話 – 2011-04-06 00:16:21

0

你可能想要做的是這樣的:

$users = array("Pete", "Jon", "Steffi"); 

$users = array_map("mysql_real_escape_string", $users); 
$users = implode(",", $users); 

..("SELECT * FROM users_newest_post WHERE FIND_IN_SET(user, '$users')"); 

FIND_IN_SET函數是低效的,但是爲了這個目的。但是,如果真的有需要,你可以轉換到IN子句,再輸入一點。

0
$sql = 'SELECT * FROM `users_newest_post` WHERE username IN (' . implode(',', $users) . ')';