User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 391,991 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,253 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 1010 | Replies: 9 | Solved
Reply
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

MySQL - guidance on multiple WHERE clause

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 166
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

Re: MySQL - guidance on multiple WHERE clause

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

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

Re: MySQL - guidance on multiple WHERE clause

  #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>
  59.  

... 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 1:39 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 166
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

Re: MySQL - guidance on multiple WHERE clause

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

If nothing is displayed, perhaps because you need to

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

I don't know if your site is setup to use <?= as a short tag for echo.
"Premature optimization is the root of all evil."
Donald Knuth / Tony Hoare
Reply With Quote  
Join Date: Jun 2008
Posts: 1
Reputation: oldcat is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
oldcat oldcat is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

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

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

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

Re: MySQL - guidance on multiple WHERE clause

  #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 1:40 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

  #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  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

  #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 1:41 pm. Reason: Code tag correction, closing tag is [/code] not [\code]
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 166
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

Re: MySQL - guidance on multiple WHERE clause

  #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  
Join Date: Jun 2008
Posts: 6
Reputation: nibbler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nibbler nibbler is offline Offline
Newbie Poster

Re: MySQL - guidance on multiple WHERE clause

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 9:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC