0

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!

1
  • 1
    Please stop writing new code with the ancient MySQL extension: it is no longer maintained and the community has begun the deprecation process. Instead you should use either the improved MySQLi extension or the PDO abstraction layer. Commented May 28, 2012 at 19:44

2 Answers 2

1

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
1

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));
5
  • With that code I am getting following error Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in Z:\www\answers\q.php on line 86 Commented May 28, 2012 at 19:31
  • yes, when I have only one results inside brand_url row I get the results, however when I have more then one e.g. dell, microsoft, hp,... I am not getting any results Commented May 28, 2012 at 19:44
  • @Kolink: why not just array_map('json_encode', $array)? The manually created function gets you nothing and wastes memory. Commented May 28, 2012 at 19:52
  • @DCoder Because I got mixed up between array_map and array_walk with regards to how many arguments are passed to the callback. That said, this way allows for easy adding of further parameters to json_encode. Commented May 28, 2012 at 19:55
  • @Kolink So how to make it work if there are more then one results inside brands_id row? Commented May 28, 2012 at 20:12

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.