Jump to content

Recommended Posts

Hi

 

Right here is what I'm trying to do.  I have in my members table 5 fields called fav_1 to fav_5.  These fields are just category ID's.  In my group table I have also got a catID (cat) field.  Im trying to make a query which will match at random the group cat to any of the members top 5 where the area first and county second are the same and limited by 5.  I also want it to order it by fav_1 and down the scale.  I have wrote the query but it just looks really messy.  Is there a nicer way to do this?

 

$qAdds = "SELECT name, cover, cat, subcat, thumbsup, FROM `groups` WHERE `cat` = ".$User['fav_1']." OR `cat` = ".$User['fav_2']." OR `cat` = ".$User['fav_3']." OR `cat` = ".$User['fav_4']." OR `cat` = ".$User['fav_5']." AND `area` = ".$User['area']." AND county = ".$User['county']." ORDER BY rand() LIMIT 5";

Link to comment
https://forums.phpfreaks.com/topic/229481-is-this-a-messy-query/
Share on other sites

Im confused by your question. If the statement you currently have producing the result set you want and are just merely asking if there is a better way to make it neater, of are you asking how to fix the logic in the statement (because it seems to me that the logic in your question does not match the logic in your statement).

 

Have you used the IN operator before ? If not check it out

http://www.w3schools.com/sql/sql_in.asp

//is this correct

$qAdds = "SELECT name, cover, cat, subcat, thumbsup, FROM `clubs` WHERE `cat` IN = ('".$User['fav_1']."', '".$User['fav_2']."', '".$User['fav_3']."', '".$User['fav_4']."', '".$User['fav_5']."') AND `area` = ".$User['area']." AND county = ".$User['county']." ORDER BY rand() LIMIT 5";

//??

You have "IN = (" it should just be "IN("

 

How about this.

 

$sql = sprintf ("SELECT name, cover, cat, subcat, thumbsup, FROM clubs 
					WHERE cat IN('%s', '%s', '%s', '%s', '%s') 
					AND area = '%s' 
					AND county = '%s' 
					ORDER BY rand()
					LIMIT 5",
					$User['fav_1'],$User['fav_2'],$User['fav_3'],$User['fav_4'],$User['fav_5'],
					$User['area'],
					$User['county']);

ok I used the code given and it seem to not be doing anything.  I dont think it is wrong because it does not throw any errors but here is what I have.  It should come back with one name....

 

$Adds =  mysql_query ("SELECT name, cover, cat, subcat, area, county, thumbsup, FROM clubs 
					WHERE cat IN('%s', '%s', '%s', '%s', '%s') 
					AND area = '%s' 
					OR county = '%s' 
					ORDER BY rand()
					LIMIT 5",
					$User['fav_1'],$User['fav_2'],$User['fav_3'],$User['fav_4'],$User['fav_5'],
					$User['area'],
					$User['county']);

		while($CP = mysql_fetch_assoc($Adds)){ 
					echo "{$CP['name']}<br/>";  }}





You have replaced the sprintf() with mysql_query(), thus your use of '%s' is being taken litteraly by the SQL. 

Don't place raw strings inside of mysql_query().  assign the string to a variable and then sanitise it fist, placing the sanitised variable inside the mysql_query(), this also has the benefit of allowing you to echo out your actual query string as it will be when it is sent to the database.

Ok.  I have made the changes and read up on the new commands you have sent me.  I re-wrote the query to match but still it echo's nothing.  I even tried just to echo $adverts but it seems to be empty.  Which would suggest the query is wrong.  I tried mysqli_query and mysql_query but nothing.  Let me explain again what I want coz maybe im going about this the wrong way.

 


// ok so I want to find all the clubs which cat of the club is equal  to the users top 5 favourite categorys.  Also in the query the returned clubs must be in either the same town or county .



$Adds =  sprintf("SELECT `name`, `cover`, `cat`, `subcat`, `area`, `county`, `thumbsup`, FROM `clubs` 
					WHERE `cat` IN('%s', '%s', '%s', '%s', '%s') 
					OR `area` = '%s' 
					OR `county` = '%s' 
					ORDER BY rand()
					LIMIT 5",
					$User['fav_1'],$User['fav_2'],$User['fav_3'],$User['fav_4'],$User['fav_5'],
					$User['area'],
					$User['county']);

					$adverts = mysqli_query($Adds) or die(mysql_error());

                                	

// I then just want to display the names of these clubs for now.  At the minute with the user im using it should display one club!  




		while($CP = mysql_fetch_assoc($adverts)){ 
					echo "{$CP['name']}<br/>";  }



// that query seems to echo nothing but I know it works because if I write........

$query = mysql_query("SELECT * FROM `clubs` WHERE `cat` = ".$User['fav_1']." ORDER BY rand() LIMIT 5")

while ($result = mysql_fetch_assoc($query)) {

echo "{$result['name']}<br/>";
}

//then my query comes back with a name.  I have check that my town and county between the club and the user are the same. So does anybody have any idea whats wrong or maybe another way to write the query to incorporate the above?  

ok I now have an error to work with.  can anyone make sense of 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 'FROM `clubs` WHERE `cat` IN('22', '20', '17', '26', '25') OR `area' at line 1

//for this query

$Adds =  sprintf("SELECT `name`, `cover`, `cat`, `subcat`, `area`, `county`, `thumbsup`, FROM `clubs` 
					WHERE `cat` IN('%s', '%s', '%s', '%s', '%s') 
					OR `area` = '%s' 
					OR `county` = '%s' 
					ORDER BY rand()
					LIMIT 5",
					$User['fav_1'],$User['fav_2'],$User['fav_3'],$User['fav_4'],$User['fav_5'],
					$User['area'],
					$User['county']);

					$adverts = mysql_query($Adds) or die(mysql_error());

		while($CP = mysql_fetch_assoc($adverts)){ 
					echo "{$CP['name']}<br/>";  }


Yeah I noticed that, mysql improved just flows my boat better :P

fair comment, but without knowing whether the mysqli library is enabled on someone elses server or not, it's probably better to stick with what flows their boat, rather than yours :P

True that. ... I'm so egoistical  :'(

Yeah I noticed that, mysql improved just flows my boat better :P

fair comment, but without knowing whether the mysqli library is enabled on someone elses server or not, it's probably better to stick with what flows their boat, rather than yours :P

True that. ... I'm so egoistical  :'(

lol  ;D

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.