Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:26:21+00:00 2026-06-16T05:26:21+00:00

Database: xx_users (id, name, user_id, email, location) xx_questions (id, question, description, user_id) Original code:

  • 0

Database:

  • xx_users (id, name, user_id, email, location)
  • xx_questions (id, question, description, user_id)

Original code:

$friends = $facebook->api('/me/friends');
$arr= $friends['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
    $friend_ids_arr[] = $friend['id'];
}

$sql = "SELECT * FROM xx_questions WHERE user_id IN (" . implode(',', $friend_ids_arr) . ") OR user_id = '$user' ORDER BY time DESC";

New code:

$friends = $facebook->api('/me/friends');
$arr= $friends['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
    $friend_ids_arr[] = $friend['id'];
}
    $sql = "SELECT *
    FROM xx_questions q JOIN xx_users u ON
        q.user_id = u.user_id
    WHERE q.user_id IN (implode(',', $friend_ids_arr)) OR // STEP 1
        q.user_id = '$user' OR // STEP 2
        u.location = (SELECT location FROM xx_users WHERE user_id = $user) // STEP 3
    ORDER BY q .time DESC";

    $data = mysql_query($sql) or die(mysql_error());
    while ($row = mysql_fetch_assoc($data)) {
        echo $row[id];
        }

I am trying to select questions from xx_questions (that have been posted by users) should any of three situations arise:

  1. Those values where the poster’s user_id matches that of one of the current user’s friends (i.e. the poster and the user are friends)
  2. Those values where the poster’s user_id matches that of the user (i.e. the poster is the user)
  3. Those values where the poster’s location matches that of the current user (i.e. the poster and user are currently in the same city)

I had originally achieved steps 1 and 2, but when I add step 3, two things happen:

  1. Step 1 stops working (i.e. only posts from the current user and people in the same city are returned)
  2. The $row[id] echoed in the while loop is that of xx_users, not xx_questions. I’ve tried $row[q.id] but to no avail.

Any idea what’s gone wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:26:23+00:00Added an answer on June 16, 2026 at 5:26 am

    In your seconds query your implode statement isnt outside the string.

    "WHERE q.user_id IN (implode(',', $friend_ids_arr)) OR" // STEP 1
    

    While php can add variable to a double quoted string ($x = "test $test test" $test will be changed to the value of $test) it can’t escape functions. So this would return in

    "WHERE q.user_id IN (implode(',', Array)) OR" // STEP 1
    

    You should use:

    "WHERE q.user_id IN (".implode(',', $friend_ids_arr).") OR" // STEP 1
    

    And in the end you are using SELECT * and if both tables have an Id column, only one is returned. Its better to use SELECT q.* to get only the questions row or perhaps SELECT q.*, u.location to get the entire question row and the location from the user table.

    I also checked your query and it should do what you want

    $sql = "SELECT q.*, u.location
                FROM xx_questions q 
                JOIN xx_users u 
                ON q.user_id = u.user_id
                WHERE q.user_id IN (".implode(',', $friend_ids_arr).") OR // STEP 1
                q.user_id = $user OR // STEP 2
                u.location = (SELECT location FROM xx_users WHERE user_id = $user) // STEP 3
                ORDER BY q.time DESC";
    

    The only thing I changed is the implode and remove the ” around $user. That is incase the $user isnt actualy an integer but a string containing a number, but has some extra spaces appended (eg ‘1 ‘ instead of ‘1’);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Database: xx_users (id, name, user_id, email, location) xx_questions (id, question, description, user_id) Code: $friends
database name customdata jobNo CustomerName Country Tel Item 1 John USA 045 Mouse 2
I fetch record from database using following code. for (LocWiseProfileBeans cn : LocWiseProfile) {
I need a query, so before entering into question I will introduce the database
i am programming in php talking to mysql database. the code requires the use
I want to connect to a remote database (SQL Server 2008) through code using
I am trying to create an email validation form for my database but am
Following is the VBA code to fetch data from oracle database to excel. Instead
I'm using the EntityManager to persist data into my database. public void save(X x){
I'm attempting to copy the database located, as an example, in: /data/data/com.x.x.x/databases/mydb.db This works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.