MySQL - guidance on multiple WHERE clause

Thread Solved

Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

MySQL - guidance on multiple WHERE clause

 
0
  #1
Jun 9th, 2008
I'm looking for some guidance on how to develop a msql query that loops through elemenets in a php array. The array is constructed from a multiple line list box.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: MySQL - guidance on multiple WHERE clause

 
0
  #2
Jun 10th, 2008
Do you want a single query where the values in your array are in the where ? Like this:

  1. $values = implode(",", $your_array);
  2. $query = "SELECT * FROM table WHERE column IN ($values)"
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #3
Jun 10th, 2008
Hi

Thanks for that. From what I understand this may well be the solution. The only problem is deploying it.

So far I'm trying ...

  1. <?php
  2.  
  3.  
  4. $Prescriber_Name = $_POST[thing];
  5. $values = implode(", ", $Prescriber_Name);
  6.  
  7. // open the connection
  8. $conn = mysql_connect("location", "username", "password");
  9.  
  10. // pick the database to use
  11. mysql_select_db("database",$conn);
  12.  
  13. //create the sql statement
  14. $query = "SELECT * FROM prescribing_test WHERE prescribing_test.Prescriber_Name IN ($values)"
  15.  
  16.  
  17. //execute the SQL statement
  18. $result = mysql_query($query, $conn) or die(mysql_error());
  19.  
  20.  
  21.  
  22. mysql_close($conn);
  23.  
  24.  
  25. ?>
  26.  
  27. <table>
  28. <tr>
  29. <th><B>Prescriber Name</B></th>
  30. <th><B>BNF Name</B></th>
  31. <th><B>Total Items</B></th>
  32. </tr>
  33.  
  34. <?
  35.  
  36. //go through each row in the result set and display data
  37. while($newArray = mysql_fetch_array($result))
  38.  
  39. {
  40.  
  41. ?>
  42.  
  43. <tr>
  44. <td><?=$newArray["Prescriber_Name"]?></td>
  45.  
  46. <td><?=$newArray["BNF_Name"]?></td>
  47.  
  48. <td>><?=number_format($newArray["Total_Items"],0,'.',',')?></td>
  49.  
  50. </tr>
  51. <?
  52.  
  53. }
  54.  
  55.  
  56. ?>
  57.  
  58. </table>

... but its not working. Can you tell me if I am on the right track here please?
Last edited by peter_budo; Jun 14th, 2008 at 2:39 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: MySQL - guidance on multiple WHERE clause

 
0
  #4
Jun 10th, 2008
Can you tell me what's not working ?

If nothing is displayed, perhaps because you need to

  1. <?php echo $newArray["BNF_Name"]; ?>

I don't know if your site is setup to use <?= as a short tag for echo.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: oldcat is an unknown quantity at this point 
Solved Threads: 1
oldcat oldcat is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #5
Jun 11th, 2008
Looks like you need to put single quotes around $value in setting the $query variable, such as follows:

  1. $query = "SELECT * FROM prescribing_test WHERE prescribing_test.Prescriber_Name IN ('$values')";

SQL Tutorial
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #6
Jun 11th, 2008
Thanks again for taking the time to help me with this.

As it stands, the code I posted doesn't display anything ( all I get is a blank page, no graphics and no text).

When I add single quotes and a semi colon to the query as suggested
  1. $sql = "SELECT * FROM prescribing_test WHERE Prescriber_Name IN ('$value')";

I get my graphics, text and table headings but unfortunately no output from the query. I've tried

  1. <?php echo $newArray["BNF_Name"]; ?> <?php echo $newArray["BNF_Name"]; ?>

but it didn't change anything.


I have added a row count to the code

  1. $num_rows = mysql_num_rows($result);
  2. echo "$num_rows Rows";

and this shows zero rows. Does this suggest that I'm not getting the data into the query in the first place?
Last edited by peter_budo; Jun 14th, 2008 at 2:40 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #7
Jun 11th, 2008
Following on from my last posting and the tweaks made, I have discovered that selecting a single entry from the multi line list box works. Full results are displayed as required. However, it is the multiple selection that is failing. With more than one line selected I get zero rows returned.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #8
Jun 12th, 2008
I've found the problem!

Adding single quotes to the comma in the implode statement now gives me the expected results.

  1. $values = implode("','", $_POST[thing]);

Not sure yet why this is needed but hey it works.

Thanks guys!
Last edited by peter_budo; Jun 14th, 2008 at 2:41 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: MySQL - guidance on multiple WHERE clause

 
0
  #9
Jun 12th, 2008
Your values are string, and therefor all need to be in separate quotes whithin the IN (...). I was expecting id's (integers) and then they are not needed. My apologies.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

 
0
  #10
Jun 12th, 2008
Hi pritaeas

No need to apologise. I didn't exactly give a lot of info at the start.

If it wasn't for you suggesting the implode option I would still be stumbling around in the dark. I really appreciate your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC