| | |
MySQL - guidance on multiple WHERE clause
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Do you want a single query where the values in your array are in the where ? Like this:
MySQL Syntax (Toggle Plain Text)
$values = implode(",", $your_array); $query = "SELECT * FROM table WHERE column IN ($values)"
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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 ...
... but its not working. Can you tell me if I am on the right track here please?
Thanks for that. From what I understand this may well be the solution. The only problem is deploying it.
So far I'm trying ...
php Syntax (Toggle Plain Text)
<?php $Prescriber_Name = $_POST[thing]; $values = implode(", ", $Prescriber_Name); // open the connection $conn = mysql_connect("location", "username", "password"); // pick the database to use mysql_select_db("database",$conn); //create the sql statement $query = "SELECT * FROM prescribing_test WHERE prescribing_test.Prescriber_Name IN ($values)" //execute the SQL statement $result = mysql_query($query, $conn) or die(mysql_error()); mysql_close($conn); ?> <table> <tr> <th><B>Prescriber Name</B></th> <th><B>BNF Name</B></th> <th><B>Total Items</B></th> </tr> <? //go through each row in the result set and display data while($newArray = mysql_fetch_array($result)) { ?> <tr> <td><?=$newArray["Prescriber_Name"]?></td> <td><?=$newArray["BNF_Name"]?></td> <td>><?=number_format($newArray["Total_Items"],0,'.',',')?></td> </tr> <? } ?> </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]
Can you tell me what's not working ?
If nothing is displayed, perhaps because you need to
I don't know if your site is setup to use <?= as a short tag for echo.
If nothing is displayed, perhaps because you need to
MySQL Syntax (Toggle Plain Text)
<?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
-- NASA
•
•
Join Date: Jun 2008
Posts: 1
Reputation:
Solved Threads: 1
Looks like you need to put single quotes around $value in setting the $query variable, such as follows:
SQL Tutorial
MySQL Syntax (Toggle Plain Text)
$query = "SELECT * FROM prescribing_test WHERE prescribing_test.Prescriber_Name IN ('$values')";
SQL Tutorial
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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
I get my graphics, text and table headings but unfortunately no output from the query. I've tried
but it didn't change anything.
I have added a row count to the code
and this shows zero rows. Does this suggest that I'm not getting the data into the query in the first place?
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
php Syntax (Toggle Plain Text)
$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
php Syntax (Toggle Plain Text)
<?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
php Syntax (Toggle Plain Text)
$num_rows = mysql_num_rows($result); 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]
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
I've found the problem!
Adding single quotes to the comma in the implode statement now gives me the expected results.
Not sure yet why this is needed but hey it works.
Thanks guys!
Adding single quotes to the comma in the implode statement now gives me the expected results.
php Syntax (Toggle Plain Text)
$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]
![]() |
Other Threads in the MySQL Forum
- Previous Thread: urgent--Dynamically populating local database
- Next Thread: get entries other than the related ones
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enter enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql syntax techsupport thunderbird transparency virtualization





