Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/
Share on other sites

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"

 

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812429
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812449
Share on other sites

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

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812475
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812495
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812498
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-812503
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/154518-array-field-and-var/#findComment-814529
Share on other sites

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.