2011-04-06 56 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) . ')';