aneeshtan Posted April 17, 2009 Share Posted April 17, 2009 hey guyz my question is how to fetch data from a field which is an array field and data saved there like this : 45,42,1 and i want to put an condition in my mysql select query WHERE $myvar is equal to one of the numbers in that field like this : mysql_query("SELECT * FROM tbl WHERE fieldArray = $myvar ); 42,43,1 = 1 but its not that simple how to do such a thing in php Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/ Share on other sites More sharing options...
ober Posted April 17, 2009 Share Posted April 17, 2009 mysql_query("SELECT * FROM tbl WHERE whatever IN (".implode(',',$myvar)."); Note: if your array values are strings, you'll need to implode with quotes around the pieces. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812422 Share on other sites More sharing options...
gurroa Posted April 17, 2009 Share Posted April 17, 2009 Solution: mysql_query("SELECT * FROM tbl WHERE ( (fieldArray = '$myvar') or (fieldArray like '$myvar,%') or (fieldArray like '%,$myvar') or (fieldArray like '%,$myvar,%') ) "); This all because of this example: lets $myvar become 1 now you want all records from your table with this fieldArray values "1", "1,42,45", "45,1,42", "42,45,1" Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812429 Share on other sites More sharing options...
ober Posted April 17, 2009 Share Posted April 17, 2009 That's just inefficient... not to mention that wasn't what the OP was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812432 Share on other sites More sharing options...
aneeshtan Posted April 17, 2009 Author Share Posted April 17, 2009 thanx for the quick answers my array that saved in database field is numbers like this : 31-41-31 and each of those indicates topic id so i want to set a condition in mysql select query if my $var is equal to one of the array data my question is how to do such a thing in select query mysql_query("SELECT * FROM tbl WHERE field IN (".implode(',',$myvar)."); is this a solution for that question !?! just pay attention that $myvar is just single number like 2 the id for my requested topic , but the field in database is an array Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812449 Share on other sites More sharing options...
gurroa Posted April 17, 2009 Share Posted April 17, 2009 My solution is really for your problem... :-) Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812450 Share on other sites More sharing options...
gurroa Posted April 17, 2009 Share Posted April 17, 2009 lets explain why this mysql_query("SELECT * FROM tbl WHERE field IN (".implode(',',$myvar)."); will never work if field value will be "42-32-4" because WHERE '42-32-4' in (4) will always fail... Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812455 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 I think you should store each number in a separate row. Storing an array does not make sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812458 Share on other sites More sharing options...
ober Posted April 17, 2009 Share Posted April 17, 2009 mysql_query("SELECT * FROM tbl WHERE field IN (".implode(',', explode('-',$myvar))."); That should do it. I didn't test it but I'm pretty sure that will get you what you want without all that extra garbage. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812459 Share on other sites More sharing options...
aneeshtan Posted April 17, 2009 Author Share Posted April 17, 2009 damn , nop that was not the answer and it dose not work , I am totally astound how to do that thing my main idea is to select data from which rows in table which it's topic_id = $var $var is clear for example a user click on this : article-topic-24.html it's obvious that $var is 24 my problem is how to find topics that has topic_id =24 but the feild topic_id is an array and can saved multiple data like = 42-43-24 I want to show topics with the topic_id =24 wow , im tired of experiencing different methods Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812475 Share on other sites More sharing options...
ober Posted April 17, 2009 Share Posted April 17, 2009 This just keeps getting worse. I think the problem is with your design, not with our methods. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812478 Share on other sites More sharing options...
gurroa Posted April 17, 2009 Share Posted April 17, 2009 Ok, try this: http://php.gurroa.cz/topics.php <?php mysql_connect('localhost', '*******', '******'); mysql_select_db('gurroa'); ?> <html> <body> <form name="test" method="post" action="topics.php"> Pick topic id:<br /> <input type="text" name="topic_id" /> <input type="submit" value="show" /> </form> <br /> <? if (isset($_POST['topic_id'])) { $id = $_POST['topic_id']; $que = mysql_query("SELECT * FROM topics WHERE ( (topics = '$id') or (topics like '$id-%') or (topics like '%-$id') or (topics like '%-$id-%') ) "); if (mysql_num_rows($que) > 0) { echo 'Topics with topic_id '.$id.'<br />'. '<table>'. '<tr><th>ID</th><th>Topics IDs</th></tr>'; while($row = mysql_fetch_assoc($que)) { echo '<tr><td>'.$row['id'].'</td><td>'.$row['topics'].'</td></tr>'; } echo '</table>'; } else { echo 'Now topics were found!'; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812495 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 Can we just be realistic here?! The idea of saving comma separated values in a fields and then calling it an "array" and then trying to query it is ridiculous. Get real - learn how normalisation works and learn what is involved in logical database design. Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812498 Share on other sites More sharing options...
aneeshtan Posted April 17, 2009 Author Share Posted April 17, 2009 hey guyz i think i did it , but it works for some topics and some not and not accurate i did it like this : $qdb = "WHERE associated IN (".implode(',', explode('-',$new_topic)).")"; and after that : $result = $db->sql_query("SELECT sid,associated FROM ".$prefix."_stories $adb $qdb $querylang ORDER BY sid DESC limit $offset, $storynum"); if u know other way that run better than this method plz tell me thanks for ur great help Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812503 Share on other sites More sharing options...
mtoynbee Posted April 17, 2009 Share Posted April 17, 2009 Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812506 Share on other sites More sharing options...
aneeshtan Posted April 20, 2009 Author Share Posted April 20, 2009 This method works for me : $new_topic IN (`associated`) look at this code ?! SELECT sid FROM db_ta WHERE topic='$new_topic' AND $new_topic IN (`associated`) AND (alanguage='$currentlang' OR alanguage='') is there any problem with the above code !? because when i add AND the code will fail to run Thank u mates Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-814529 Share on other sites More sharing options...
aneeshtan Posted April 21, 2009 Author Share Posted April 21, 2009 no one is here ! hey guys i hope u just give me a clue and the problem would be solved thanx Quote Link to comment https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-815244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.