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 8260467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:06:22+00:00 2026-06-08T03:06:22+00:00

I have the following sql statement, which works perfect and since I’m transferring everything

  • 0

I have the following sql statement, which works perfect and since I’m transferring everything to PDO, I’ve having the worst time trying to convert this complicated statement. Here it is working below:

$sql_res=mysql_query("SELECT a.first_name, a.last_name, a.uid, a.hometown, 
IF(b.friend_two IS NULL, 0, 1) AS isFriend FROM users a LEFT JOIN friends 
b ON a.uid = b.friend_two AND b.friend_one = $uid WHERE  
CONCAT(a.first_name, ' ', a.last_name) LIKE '%$q%' AND a.uid <> $uid 
ORDER BY isFriend DESC");

I’ve been trying for hours to convert and this is all I could get from another S.O example (it’s not even close to working)

Any help is wonderfully appreciate. Oh and also $q is a POST variable from the user so PDO is a must in my case.

UPDATED Code (2nd time):

$sql = "select a.first_name, a.last_name, a.uid, a.hometown, 
IF(b.friend_two IS NULL, 0, 1) AS isFriend FROM users a LEFT JOIN 
friends b ON a.uid = b.friend_two AND b.friend_one = :friend_one 
where ( first_name like concat('%', :fname, '%') or last_name like 
concat('%', :lname, '%') ) and a.uid <> :uid ORDER BY isFriend DESC"; 
$stmt= $db->prepare($sql); $stmt->bindValue(':fname', "%$q%", PDO::PARAM_STR); 
$stmt->bindValue(':lname', "%$q%", PDO::PARAM_STR); 
$stmt->execute(array(':friend_one' => $uid,':uid' => $uid));

This works without any error but fails to produce the same results as the original sql statement.

  • 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-08T03:06:23+00:00Added an answer on June 8, 2026 at 3:06 am

    removed the extra from and put () around your name filter, as
    A OR B AND C = A OR (B AND C), and i gues you wanted (A OR) B AND C

    select
      a.first_name,
      a.last_name,
      a.uid,
      a.hometown,
      IF(b.friend_two IS NULL, 0, 1) AS isFriend 
    FROM users a 
    LEFT JOIN friends b
      ON a.uid = b.friend_two AND b.friend_one = :uid
    where 
      (
        first_name like concat('%', :fname, '%') or 
        last_name  like concat('%', :lname, '%') 
      ) and
      a.uid <> :uid 
    ORDER BY
      isFriend DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have the following SQL Case statement which works perfectly as long as the
I have the following insert statement which works fine on one system running SQL
I have the following SQL statement which has a very bad performance: SELECT frmInstLastModifiedDate
I am having trouble with the following SQL statement. I have had this issue
I have the following filter query which is doing an SQL OR statement: results
I have the following SQL statement which was working perfectly until I moved it
I have a SQL statement which works when executed in MS Server Management Studio
Hi I have following sql statement that gives me the the first_name and total
I have the following SQL statement: SELECT ID, NAME FROM myTable WHERE ID LIKE
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as

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.