Andreu Posted April 22, 2007 Share Posted April 22, 2007 Hi, I have done the following query: $query = sprintf ("SELECT * FROM users WHERE country LIKE '$selCountry'"); $result = mysql_query($query,$link); I want to obtain the users that are from a certain country but when $selCountry is equal to "All" I want to obtain all of them. I have tried the query in mysql webpage using '%' and it works but when using this character from the php file it doesn't work. Could someone help me? Thanks!!! Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/ Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 <?php if ($selCountry == 'All') $query = "SELECT * FROM users"; else $query = "SELECT * FROM users WHERE country = '$selCountry'"); ?> Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235434 Share on other sites More sharing options...
Trium918 Posted April 22, 2007 Share Posted April 22, 2007 <?php if ($searchtype == "all_country"){ $query="SELECT * FROM table_name"; }else{ $query="SELECT * FROM table_name where country='$searchtype'";} ?> Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235435 Share on other sites More sharing options...
Andreu Posted April 22, 2007 Author Share Posted April 22, 2007 Hi, thanks for the quick reply The problem of this "if" is that it works fine if you have to do it just with one but if you have to do it with four, the code gets a bit heavy. Is there any value to assign to the parameters equivalent to "*". Code: $query = sprintf ("SELECT * FROM tandem WHERE country LIKE '$selCountry' AND city LIKE '$selCity' AND lang1 LIKE '$iWantToSpeak' AND lang2 LIKE '$iSpeak'"); CHEERS Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235436 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 LIKE '%' is the SQL wildcard Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235438 Share on other sites More sharing options...
Andreu Posted April 22, 2007 Author Share Posted April 22, 2007 Hi, what I don´t understand is why this solution of using % works in the web based query (phpMyAdmin) and it doesn´t when using a php file? What would be your advice to solve the problem without using a series of "if"'s? Thanks Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235445 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 Why do you insist on sprintf() when you are not formatting any variables? To put "%" (which is sprintf escape character) into a sprintf format string you need "%%" Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235449 Share on other sites More sharing options...
Andreu Posted April 22, 2007 Author Share Posted April 22, 2007 Thanks! The problem is solved. Very Useful Link to comment https://forums.phpfreaks.com/topic/48159-php-query-which-value-to-use-to-select-all/#findComment-235491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.