Jump to content

Recommended Posts

The problem I have is using a variable array in a MYSQL IN condition.

 

$station_equipment=array(1,2,3,4,5);
$weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' and id IN ('$station_equipment') ORDER BY id ASC");

 

This returns an empty set where as:

 

$weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' and id IN (1,2,3,4,5) ORDER BY id ASC");

 

Works fine.  Any suggestions?

 

Link to comment
https://forums.phpfreaks.com/topic/173107-solved-trouble-with-mysql-in/
Share on other sites

You would need to post your current code and an example of what the data in the table looks like.

 

You Really don't want me to do that It is huge, complicated, and filled with includes and ajax calls and all of it works.

 

The only thing that doesn't work is the query using IN.  It works fine without it, (just doesn't screen the results).  It also works fine if I manually add the data in a string.  The problem arises when a variable is used with IN.

 

I'll create a simplified section to describe my problem:

 

Pertinent table data:

 

ID = INT(11);

 

Table: equipment

ID  category

1  weapon

2  weapon

3  weapon

4  weapon

5  weapon

 

<?php
include "common/head.php";
$station_equipment=array(1,2,3,4,5);
$station_equipment2=implode(",",$station_equipment);
$weapons=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN (1,2,3,4,5) ORDER BY id ASC");
$weapons2=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN ('$station_equipment') ORDER BY id ASC");
$weapons3=mysql_query("SELECT * FROM equipment WHERE category='weapon' AND id IN ('$station_equipment2') ORDER BY id ASC");
$result=mysql_num_rows($weapons);
$result2=mysql_num_rows($weapons2);
$result3=mysql_num_rows($weapons3);
echo"result = $result, result2 = $result2, result3 = $result3";
?>

 

The result is this:

 

result = 5, result2 = 0, result3 = 1

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.