0

I have a small task where I have a mysql table "shops".It contains a column "categories". Each field of categories contains diiferent values like "22,44,33,55,24,33,22" Now taking each value from that field, i need to get the value from a column "parent" in another table. (linking with ids) I am selecting the whole string, but i want to select each number. Please help me with this.

$db_selected = mysql_select_db("",$con);
$sql = "SELECT categories from shops";
$array = mysql_query($sql,$con);
while($row=mysql_fetch_array($array)){
foreach($row as $value){
    $result= explode(" ", $value);
    foreach($result as $newvalue){
    $query="SELECT parent FROM categories where categories.id=$newvalue<br/>";
    echo $query;
    }
    }
    }
mysql_close($con);
?>
4
  • 3
    And this is why you normalize your database with a separate shops_categories table to record that relationship instead of stuffing comma-separated numbers into the same field. Commented Oct 16, 2012 at 8:52
  • im sorry,i cant normalize my database but need a solution for it... Commented Oct 16, 2012 at 8:54
  • check MySQL function find_in_set Commented Oct 16, 2012 at 8:56
  • Is 22,44,33,55,24,33,22 a data in a field in a single row and data in multiple rows? Commented Oct 16, 2012 at 8:58

5 Answers 5

2

You are exploding based on a space charater but your value needs to be exploded on the basis of ,. So try that

$result= explode(",", $value);
foreach($result as $newvalue){

    $query="SELECT parent FROM categories where categories.id='$newvalue'";
                                                           // ^ Quotes the Value
                                                           // Remove the <br />

    echo $query."<br />"; //Instead add it here and avoid the bug if you decide the run the query

    // This example is showing mysql_* library but it is deprecated

    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $parent = $row['parent']; //Now you can something like this


}
Sign up to request clarification or add additional context in comments.

3 Comments

@user1694724, Added an example
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given...got an error
@user1694724, Can you update the question with what you have tried?
0

If your categories column contains "22,44,33,55,24,33,22" then surely your explode should be

$result= explode(",", $value);

eg explode on the comma in the string to give you 22, 44, 33 ...

1 Comment

If you updated your code with the change we all suggested, you should see it print out your queries as you asked..
0

I think the problem is here $result= explode(" ", $value); It should be $result= explode(",", $value);

1 Comment

make another query using the values extracted from $result.
0
$result= explode(",", $value);

Since the format of the string is separated by comma, should explode the string by comma instead.

Comments

0

Are the values separated by commas?

Even though your problem is a database design, explode(',', $value) should get you the IDs.

1 Comment

Well, you already have the categories ids that that category is related to, with that id you could query the same table, but, as I said, a better table design must be a better approach.

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.