PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old July 21st, 2002, 08:58 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
dynamically generate mysql queries

Hi all, I have a form which submits 5 variables to the script below. I would like to build some sort of array or way to catch all the variables sent as null or blank (the user left a field blank) and not include those variables in the mysql query. I have been messing with the idea of building an array of the input variables and using foreach to eliminate the null values, however it doesnt seem to be working.

ANY HELP AT ALL GREATLY APPRICATED, im stumped and going crazy =)



$query = '';

foreach($articlename && $articlenum && $date && $partnum && $model as $key => $value)
{
if(!empty($input[$key]))
{
$query .= "AND $key = $value";
}
}

mysql_connect (host,user,pass);
$result = mysql_db_query ("db","SELECT * FROM articles WHERE $query");

Reply With Quote
  #2  
Old July 22nd, 2002, 12:13 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: dynamically generate mysql queries

Your building your query like this:
SELECT * FROM articles WHERE AND ...

HTH.

Reply With Quote
  #3  
Old July 23rd, 2002, 05:11 AM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: dynamically generate mysql queries

Quote:
foreach($articlename && $articlenum && $date && $partnum && $model as $key => $value)


This won't work I think. You appear to be bitwise ANDing the values together.

I think what you want is:
php Code:
Original - php Code
  1.  
  2. <?php
  3. //this is the way I usually see the dynamic array in a foreach
  4. foreach(array($articlename, $articlenum, $date, $partnum, $model) as $value){
  5.  
  6. }
  7.  
  8. //this is closer to what you want, though I am not sure if it will work
  9. foreach(array('articlename'=>$articlename, 'articlenum'=>$articlenum, 'date'=>$date, 'partnum'=>$partnum, 'model'=>$model) as $key=>$value){
  10.   //your code here
  11. }
  12. ?>


Cheers,
Keith.


Quote:
Hi all, I have a form which submits 5 variables to the script below. I would like to build some sort of array or way to catch all the variables sent as null or blank (the user left a field blank) and not include those variables in the mysql query. I have been messing with the idea of building an array of the input variables and using foreach to eliminate the null values, however it doesnt seem to be working.

ANY HELP AT ALL GREATLY APPRICATED, im stumped and going crazy =)



$query = '';

foreach($articlename && $articlenum && $date && $partnum && $model as $key => $value)
{
if(!empty($input[$key]))
{
$query .= "AND $key = $value";
}
}

mysql_connect (host,user,pass);
$result = mysql_db_query ("db","SELECT * FROM articles WHERE $query");



Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > dynamically generate mysql queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT