Boris Zegarac
Boris Zegarac

Reputation: 531

Using Array results in MySQL query

I have the following query:

$query_q_pass = sprintf("SELECT * FROM answers INNER JOIN users WHERE a_id = %s and answers.user_id = users.user_id");
$q_pass = mysql_query($query_q_pass, $cd) or die(mysql_error());
$row_q_pass = mysql_fetch_assoc($q_pass);

Now the answers table from above query has a row brand_url which has many different values separated with commas, e.g. dell, microsoft, hp, ...

What I want to do is select all those values from the brand_url row and add them in another query where I will extract all brands that exist inside brand_url. This is the other query:

$query_conn_brands = 'SELECT * FROM brands WHERE brand_url = 'VALUE FROM ABOVE QUERY'';
$conn_brands = mysql_query($query_conn_brands, $cd) or die(mysql_error());
$row_conn_brands = mysql_fetch_assoc($conn_brands);
$totalRows_conn_brands = mysql_num_rows($conn_brands);

I tried many diferent ways using arrays but all I get is all results from the brands table. This is my last unsuccessful try:

$brand_pass_ids = $row_q_pass['brand_id'];
$array = array($brand_pass_ids,);

$query_conn_brands = 'SELECT * FROM brands WHERE brand_url IN (' . implode(',', array_map('intval', $array)) . ')';
$conn_brands = mysql_query($query_conn_brands, $cd) or die(mysql_error());
$row_conn_brands = mysql_fetch_assoc($conn_brands);
$totalRows_conn_brands = mysql_num_rows($conn_brands);

From the above example all I get is ALL results from brands table and not only those that exist inside answers table. I really hope someone will help me with this.

Thanks a lot!

Upvotes: 0

Views: 314

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

The reason it's failing is because your query will look like:

SELECT * FROM brands WHERE brand_url IN (dell,microsoft,hp)

There are string values, and so they must be handled as strings. Try:

implode(",",array_map(function($a) {return json_encode($a);},$array))

Although encoding as JSON may seem crazy, it's actually a good choice here. Strings are wrapped in quotes and escaped as needed, numbers are left as they are, and null is also handled correctly.

EDIT: For older versions of PHP that don't support anonymous functions:

implode(",",array_map(create_function('$a','return json_encode($a);'),$array));

Upvotes: 1

eggyal
eggyal

Reputation: 125865

Use MySQL's FIND_IN_SET() function:

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

mysql> SELECT FIND_IN_SET('b','a,b,c,d');
        -> 2

In your case:

SELECT * FROM brands WHERE FIND_IN_SET(brand_url, ?)

But you could join your queries together if so desired:

SELECT *
FROM   answers
  JOIN users  USING (user_id)
  JOIN brands ON FIND_IN_SET(brands.brand_url, answers.brand_url)
WHERE a_id = %s

Upvotes: 1

Related Questions