2

I'm using the following code to sort MySQL queries into time/date:

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']);
  }

instead of having the PHP run through and show all the values in the table can I have it show the values from an array?

4
  • 1
    You mean show only specific users? Commented Apr 6, 2011 at 0:11
  • Then build the SQL query in such a way as to return only those users you are interested in. Look up syntax similar to SELECT ... WHERE user IN ('foo', 'bar'). Commented Apr 6, 2011 at 0:13
  • 1
    I think your social network will crash if you continue using SELECT * like this. Commented Apr 6, 2011 at 0:28
  • haha how'd u know I was building a social network? Commented Apr 6, 2011 at 1:04

4 Answers 4

1

So, you want to find specific users in the SQL query to return? Build your query programmatically:

$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);

Which would get you a query like:

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

So, you want to find all posts for a certain date or between two dates, kinda hard to do it without knowing the table structure, but you'd do it with something like this:

//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
}

To find posts for a specific user you would modify the statement to be something like:

$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"
Sign up to request clarification or add additional context in comments.

14 Comments

What if I needed all of those values?
How do you mean? Which values?
All of them: John, Pete Allport, etc, I am trying to pull the user's friends from a table of all users if that makes sense?
How is the table structured so that you know who the user's friends are?
haha I can't tell you that but I'm going to try and implement your example with some of my pages and see how it works as long as I can use all of those
|
0

To dump the result into an array do the following:

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
} 

1 Comment

No no, This is good except I need to be able to be able to load an array into the query, if that makes sense
0

What you probably want to do is this:

$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')");

The FIND_IN_SET function is a but inefficient for this purpose. But you could transition to an IN clause with a bit more typing if there's a real need.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.