1

Example i'm getting 2 variable from my db.($v1, $v2)

SELECT * FROM mytable WHERE id=$1 AND  $v2 

But in another example im getting 3 variable from db ($v1, $v2, $v3)at same query cause i proggramed it like it. In this condition how can i program WHERE statement of sql query

edit

WHERE id=$v1 AND $v2 AND $v3 // i want to add it And conditions automaticly
3
  • You want to know if id is equal to more than one value? What's in $v2 or $v3? Are you adding SQL string conditions in those variables? Or actual data? Commented Feb 15, 2014 at 21:32
  • id=x and y doesn't make much sense. Is id supposd to be equal to both of those value, or should id be equal to the logical AND of the two values? Commented Feb 15, 2014 at 21:32
  • I never use AND condition shoul i write id=$v2 again.whatever this is not the question the question is how can i program in a sql query Commented Feb 15, 2014 at 21:40

2 Answers 2

1

If you need to find any records which contain any of your variable you can use OR as follow

SELECT * FROM mytable WHERE id = $v1 OR id = $v2 OR id = $v3
Sign up to request clarification or add additional context in comments.

5 Comments

Okay i ll use OR butb real question is different
What's real question then?
Isnt my question clear? another time i ll get 8 value and another just 2 again i want to add it this or conditions automaticly
Man you need to elaborate your code, maybe create an array of ids and loop throw it and build the query according
How to loop in an sql query
1

If you are trying to select multiple ID values, use IN conditional:

SELECT * FROM mytable WHERE id IN ($v1, $v2, $v3)

EDIT: Asked for a flexible way to add values:

Assuming that this query is set by a function and this function will receive all ID values that should be in where clause. Something like:

<?php

$example_ids = array(1, 5, 7, 15, 22);

wherein($example_ids)

function wherein($ids){
    // Select part
    $query = "SELECT * FROM mytable ";

    // Where statement
    $where = " WHERE id IN (";

    // For loop to use each value sent
    // as a value in IN (...
    for ($i=0; $i < count($ids); $i++) { 
        // Eg: $i = 0
        // $ids[$i] = 1
        // 
        // So:
        // WHERE id IN (1
        $where .= $ids[$i];

        // If its not the last value then
        // add a comma for SQL syntax
        if ($i < count($ids) - 1)
            /// Where id IN (1,
            $where .= ",";
    }

    // At the end of the loop, $where must be
    // something like:
    // WHERE id IN (1,5,7,15,22
    // 
    // Once again, for SQL syntax, the close )
    $where .= ")";

    // Finaly, the last step, putting all together:
    // SELECT * FROM mytable  WHERE id IN (1,5,7,15,22)
    $query .= $where;
}

4 Comments

But another time i ll get 8 value and i want to put them right there with a program in sql query how can i do that
Then you must: 1. Organize ids in an array 2. In a for loop, concatenate each variable with a comma 3. Check for the last value to prevent SQL syntax error 4. Close where clausule 5. Concatenate major query with this where variable. I'll update the code.
Okay i get the your answer IN is what i need. update the code
Have in mind that you should use prepared statements to prevent SQL Injection. I believe that you can adapt a similar solution with that. Read more about at: php.net/pdo.prepared-statements

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.