Jump to content

PHP MYSQL Search with multiple WHERE conditions


jattsurma

Recommended Posts

Hello Everyone,

 

I'm new here so "Hi" to all the php freaks out there. I'm new to php as well but I'm getting hang of things pretty quickly. I have one issue that I'm struggling with so your help will be greatly appreciated. Here is my problem:

 

I'm trying a create a simple boolean search function with multiple WHERE conditions. Here is the code:

if($_GET['terms']){
$terms = $_GET['terms'];
$myAgents = "SELECT * FROM userdata WHERE MATCH(fName,lName) AGAINST('$terms' IN BOOLEAN MODE) AND WHERE account_type='agent' AND WHERE manager_id='".$_SESSION['manager_id']."' ORDER BY fName ASC";
//echo $myAgents;
mysql_query("$myAgents") or die(mysql_error());
}

 

But for some reason it is not working and I'm getting this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE account_type='agent' AND WHERE manager_id='222222' ORDER BY fName ASC' at line 1

 

Can someone please tell me what am I doing wrong? Thanks in advance for your suggestions and help!

 

Jatt Surma

 

Link to comment
Share on other sites

Hello Maq,

 

Thanks a lot! That took care of the error part.

 

for some reason, it is not working. I'm not getting any errors neither any results. I know all the variables have the right values as well as the MYSQL table.

 

Any other suggestions? Thanks again for your help.

 

Jatt Surma

Link to comment
Share on other sites

Hello Maq,

 

Thanks again! I have 2 records in MYSQL table and they both match with the query. Here is result of $myAgents echo:

 

SELECT * FROM userdata WHERE MATCH(fName,lName) AGAINST('John' IN BOOLEAN MODE) AND account_type='agent' AND manager_id='222222' ORDER BY fName ASC

 

Here is rest of the code:

 

<?php
echo'<div align="left"><form name="search" method="get" action="'.$site_url.'portal/my-agents.php?do=search'.'">
<input type="text" name="terms" size="20" value="'.$_GET['terms'].'"><input type="submit" value="Find">
</form></div>
';
if($_GET['terms']){
$terms = $_GET['terms'];
$myAgents = "SELECT * FROM userdata WHERE MATCH(fName,lName) AGAINST('$terms' IN BOOLEAN MODE) AND account_type='agent' AND manager_id='".$_SESSION['manager_id']."' ORDER BY fName ASC";
//echo $myAgents;
mysql_query($myAgents) or die(mysql_error());
}
else{
$myAgents = mysql_query("SELECT * FROM userdata WHERE account_type='agent' AND manager_id=".$_SESSION['manager_id']);
}
$tMyAgents = @mysql_num_rows($myAgents);
echo'
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td valign="top" align="left">Name:</td>
<td valign="top" align="left">Leads:</td>
<td valign="top" align="left">Contracts:</td>
<td valign="top" align="left">Status:</td>
</tr>';
for($i=1;$i<=$tMyAgents;$i++){
$agent = mysql_fetch_array($myAgents);
$leads = mysql_num_rows(mysql_query("SELECT project_id FROM projects WHERE agent_id=".$agent['agent_id']." AND status='LEAD'"));
$contracts = mysql_num_rows(mysql_query("SELECT project_id FROM projects WHERE agent_id=".$agent['agent_id']." AND status='CONTRACT'"));
echo'<tr>
<td valign="top" align="left">'.$agent['fName'].' '.$agent['lName'].'</td>
<td valign="top" align="left">'.$leads.'</td>
<td valign="top" align="left">'.$contracts.'</td>
<td valign="top" align="left">'.$agent['status'].'</td>
</tr>';
}
echo'</table>';

?>

 

As you can see that there are 2 queries 1 with search and 1 without search. One without search works perfectly. So its something with the search query.

 

Thanks again! I appreciate your help.

 

Jatt Surma

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.