0

i have an array in databae like this :

    Field : smt_id
    Value in database : 261, 323
    type : varchar(100)

smt_id is not fix field. if i updated as 3 arrays it will be like this example : 261, 323, 111 and variable $mid will change to 261, 323, 111

now i want to do sql statement like this using this variable.

$mid = 261, 323

$sql = "SELECT first_name FROM ".TABLE_PREFIX."members WHERE member_id ='$mid'";
$result = mysql_query($sql,$db);

when i print the sql the result is like this:

SELECT first_name FROM AT_members WHERE member_id ='261, 323'

My question is, how i change above sql so it become like this :

SELECT first_name FROM AT_members WHERE member_id IN ('261', '323')

and how can i make every result break like this:

John 
Merry
9
  • Always just 2 values separated by a comma? varchar data type? Commented Aug 6, 2014 at 1:44
  • no i inserted the database as an array so it can be updated more than two Commented Aug 6, 2014 at 1:45
  • what is the data type of the column smt_id? Commented Aug 6, 2014 at 1:46
  • Assuming this $mid = 261, 323 is your actual code, you'd need to do $mid = array('261', '323'); and then explode on the array. That may also be implode or even in_array(). There are a many ways of doing this, but you get the general idea ;) Commented Aug 6, 2014 at 1:59
  • yup, creating a string with desired separator from an array is implode Commented Aug 6, 2014 at 2:01

3 Answers 3

1
$mid = explode(',', preg_replace('/\s+/', '', $mid));
$sql = 'SELECT first_name FROM '.TABLE_PREFIX.'members WHERE member_id IN ('.implode(',', $mid).')';
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best approach for the current case, as $mid is normalized in a separate step before being provided to the SQL query. But $mid should be an array beforehand, not at this last moment ;)
0

Asserting $mid is an array:

'WHERE member_id IN ('.implode(',', $mid).')'


It also works if the array has only one value.


Just as a reminder, code like that is vulnerable to SQL injection.

5 Comments

hi thanks. your code ok but no row return.. MySQL returned an empty result set (i.e. zero rows)
because your variable $mid is not an array most likely
If $mid is a string like '261, 323' you are doing something wrong.
hurmm.. maybe.. because when i echo it will be ex: 123, 234 ... HOW CAN I CHANGE '261, 323' <-- VARCHAR to ('261', '323') ... do you know how?
Don't confuse PHP types and MySQL types. There are 2 separate worlds. Then, you rather should use var_dump() for debugging. Here you'll likely see your variable is a string. So just put it like so: 'WHERE IN ('.$mid.')'. But this is dirty, $mid should be an array, consider reviewing your preceding code.
0

stop using mysql extension

it is deprecated. use mysqli or PDO instead(sql injection alert). Moreover don't push your variables directly to the query, use prepared statements. Check here. Imploding is well explained in Double Gras' answer. For your second question, if you want every result one by one, you should fetch them one by one. fetch_assoc is good here for mysqli extension.

simple usage of fetch_assoc:

if ($result = $mysqli->query($query)) {

        /* fetch associative array */
        while ($row = $result->fetch_assoc()) {
            printf ("%s (%s)\n", $row["column1"], $row["column2"]);
        }

3 Comments

Indeed, running the query (which is done on the SQL server) then fetching the results (at the pace you want, consumes network traffic) are 2 distinct operations.
While I agree with the sentiments, this answer does not answer the OPs question - nor does it show proper multi-parameter binding. If an actual answer is not provided, consider a comment.
and how can i make every result break like this:

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.