0

I'm trying to query all results where surveyid contains my array $subscibearray. The problem is the SQL query is only returning the first result (44 in this case) instead of all of them. I tried using mysql_fetch_array as well without any success. My results are being outputted via Table. Any help would be appreciated. I omitted as much code as possible to make this easy to read.

Array format:

$subscribearray = "" . join(', ',$subscribearray) . "";  

var_dump of $subscribearray:

string(17) "'44, 35, 194, 36'"

query:

$result = mysql_query("SELECT * 
                       FROM surveys 
                       WHERE surveyid IN ($subscribearray) 
                       ORDER BY peercompletetime DESC 
                       LIMIT 100") 
or die(mysql_error());
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
alicia233
  • 27
  • 1
  • 7
  • Please add code how you output your records. – u_mulder Aug 24 '16 at 18:52
  • 1
    And also explain - why you put ids in `'`? – u_mulder Aug 24 '16 at 18:53
  • Should I remove them? Sorry. – alicia233 Aug 24 '16 at 18:53
  • 1
    Yes, that should help. – Don't Panic Aug 24 '16 at 18:54
  • you should also switch over to MySQLi, MySQLi is MySQL Improved. The reason behind this is because MySQL has been deprecated since around PHP 5. MySQLI is more secure and such. It isn't that hard, for example mysql_query becomes mysqli_query (note the i after mysql). – Tyler Aug 24 '16 at 18:56
  • Tyler I will take this into consideration. Thank you. – alicia233 Aug 24 '16 at 18:56
  • 1
    Every time you use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), **a Kitten is strangled somewhere in the world** it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 24 '16 at 18:56
  • 1
    I love kittens this is terrible. Thanks for the link. – alicia233 Aug 24 '16 at 18:57
  • what do you get when you var_dump the $result? I'm mostly interesting in the num_rows and field_count values – Tyler Aug 24 '16 at 18:58
  • If you think about how you would code an `IN()` query it would look like this `WHERE surveyid IN (1,2,3,4,5)` **and not** `WHERE surveyid IN ('1,2,3,4,5')` so that is what you have to mimic in your php statement that preps the data for this variable – RiggsFolly Aug 24 '16 at 18:59
  • Let me var_dump the num_rows and find out thanks – alicia233 Aug 24 '16 at 19:01
  • Of course every retest kills another kitten – RiggsFolly Aug 24 '16 at 19:01

1 Answers1

1

You should:

$subscribearray = join(',', $subscribearray); // 1,2,3

Or:

$subscribearray = "'" . join("','", $subscribearray) . "'"; // '1','2','3'

If you wrap all ids with a string, it will look for the string, and not separated values. ('1,2,3')

Of course, if it's a numeric column you should use the first one.

HTMHell
  • 5,761
  • 5
  • 37
  • 79