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

  • Home
  • SEARCH
  • 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 6828473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:23:14+00:00 2026-05-26T22:23:14+00:00

mysql_query(SELECT * FROM foo WHERE id =’$foo’ OR id = ‘$foo2); This doesn’t work.

  • 0
mysql_query("SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2");

This doesn’t work.

Basically, I want to be able to select it where the id is one variable’s value OR another one’s.

Thanks.

EDIT: The ID column is numerical.

  • 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-05-26T22:23:15+00:00Added an answer on May 26, 2026 at 10:23 pm

    As others have said and you confirmed, the problem is that you are using string literals to compare to a numeric column. To have it work, the query should look like

    mysql_query("SELECT * FROM foo WHERE id =$foo OR id = $foo2");
    

    However, this solution has very very bad code smell!

    First off, this is why IN exists: to be able to write

    mysql_query("SELECT * FROM foo WHERE id IN ($foo, $foo2)");
    

    And second, are you injecting unescaped strings into your query? If you are, your code is vulnerable to sql injection! Escape and quote your variables to be safe, like this (in the general case):

    $query = sprintf("SELECT * FROM foo WHERE id IN ('%s', '%s')",
                     mysql_real_escape_string($foo),
                     mysql_real_escape_string($foo2));
    mysql_query($query);
    

    or alternatively like this, since in this specific scenario you know we ‘re talking about integer values:

    $query = sprintf("SELECT * FROM foo WHERE id IN (%s, %s)",
                     intval($foo), intval($foo2));
    mysql_query($query);
    

    Footnote: I am aware that when using sprintf like this, one could also handle integer values by just using %d instead if %s as the format specifier. However, I believe that proving you are correctly escaping variables should be possible by just looking at one place (the parameter list) instead of multiple places (did I use intval on the variable? or maybe I did not, but I ‘m using %d in the format string so I ‘m still OK?). It may sound counter-intuitive, but it’s more robust in the face of modifications.

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

Sidebar

Related Questions

I have a query that is basically like this: SELECT foo FROM bar where
This is my code: $query = mysql_query(SELECT * FROM books ORDER BY id) or
say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');
Say, this is my query $stmt = SELECT * FROM foo WHERE x=?; How
Let's say I have this query in PHP. $a = mysql_query(SELECT * FROM `users`
$result = mysql_query(SELECT * FROM media WHERE path = '$target'); if($row = mysql_num_rows($result)==1) {
$query = mysql_query(SELECT * FROM news WHERE id = '{$_GET['id']}'); $news = mysql_fetch_assoc($query); $sql84
$result = mysql_query(SELECT * FROM project ORDER BY projectid); while($row = mysql_fetch_array($result)) { return(array($row['projectid'],
function showFields($selClient) { $result = mysql_query(SELECT * FROM project WHERE projectid = $selClient); $values
include(conn.php); $result = mysql_query(SELECT * FROM sggame); while($row = mysql_fetch_assoc($result)); { $id = $row['id'];

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.