Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
mysql query with internal php loop
Old 05-11-2007, 10:08 PM mysql query with internal php loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Hi

Can anybody tell me if it's possible to run a php loop inside a mysql query please? It seems quite a logical thing to do but i can not get it to work - somthing along the lines of this:

PHP Code:
$query="SELECT * FROM table WHERE ".
$e='0'
while (
$e5)
{
.
"column='$e' and".
$e++;
}
.
" id>'0'"
Thanks

Last edited by hiptobesquare; 05-11-2007 at 10:13 PM..
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-12-2007, 08:27 AM Re: mysql query with internal php loop
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
How are you going to run PHP inside a SQL query????

SQL servers don't "do" PHP.

You have to build the query then send it, if you need a loop in a query it will have to be coded in a stored procedure (MySQL 5+) and call that.

in your code you have a few problems

$e is declared as a string, incrementing a string is not going to have the effect you would expect.
You have no space in your concatenated code so it would end up as
... WHERE column='0' andcolumn='1' andcolumn='5' andcolumn='3' andcolumn='4' andcolumn='5' and id>'0'
Which as you can tell would raise an error

not 100% sure what you are trying to achieve,
but it seems to me that;

select fieldlist from table WHERE column <= 5 and id = 0 ;

would do exactly what the code above is attempting
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-12-2007, 10:51 AM Re: mysql query with internal php loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
sorry, bad example of what im trying to achieve.

I have a form field where a user can enter more than one search term if
desired, seperated by commas.

I need to run a sql query which will loop a "column LIKE '%$term%' OR" for the number of search terms.

The query would look something like this:

PHP Code:
$querystart="SELECT * FROM table WHERE "
$soundslik explode(","$soundslike_master); 
foreach(
$soundslik as $el => $soundslik
{  
  
$term str_replace(" ""_"trim($soundslik)); ."bandname LIKE '%$term%' or".  
el++; 


.
"some other search criteria"
Any Ideas?
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-12-2007, 01:55 PM Re: mysql query with internal php loop
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
That should build the query ok (didn't test it)

you will need a space before "bandname" and after "or" in the concatenation. ( ." bandname LIKE '%$term%' or ". )

And be aware of the underscore ("_") being a single character wildcard when using LIKE queries.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-12-2007, 04:06 PM Re: mysql query with internal php loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
duplicate

Last edited by hiptobesquare; 05-12-2007 at 04:29 PM.. Reason: posted my reply twice
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-12-2007, 04:11 PM Re: mysql query with internal php loop
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
For some reason it trips up at '.' when i output the resluts of my loop in the if statement shown below. I cant figure out why. I have tied alot of variations on this and get an error either at the { or at one of the '.' s.

Confused

PHP Code:
   $querystart="SELECT * FROM band WHERE ".
  
$soundslik explode(","$soundslike_master);
  foreach(
$soundslik as $el => $soundslik)
  { 
    
$term str_replace("_"" "trim($soundslik));
    if(
$el=='0'){  ." bandname LIKE '%$term%' ".}
    else        {.
"OR bandname LIKE '%$term%' ".}
  }  
.
""

Last edited by hiptobesquare; 05-12-2007 at 04:29 PM..
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-14-2007, 02:03 AM Re: mysql query with internal php loop
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
keep echoing out the SQL string while it is being concatenated so you can see what is happening
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-17-2007, 06:44 PM Re: mysql query with internal php loop
Average Talker

Posts: 29
Name: Amit Soni
Trades: 0
//Try this...


$querystart
="SELECT * FROM band WHERE ";

$soundslik = explode(",", $soundslike_master);
foreach(
$soundslik as $el => $soundslik)
{
$term = str_replace("_", " ", trim($soundslik));
if(
$el=='0')
{
$querystart.=" bandname LIKE '%$term%' ";
}
else
{
$querystart.="OR bandname LIKE '%$term%' ";
}
}
$result=mysql_query($querystart);

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Reply     « Reply to mysql query with internal php loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.96754 seconds with 12 queries