I have a table called country_zones and theres a column called country which currently holds
["canada","United States"]
I first, extract the users country they signed up with and then need to do a query to see which users country matches the row of country_zones. Although whenever I do that I either get error or NULL
Ive tried this.
$country = json_decode($this->db->get_where('country_zones',array('country'=>$user_country)));
also.
$country = $this->db->query("SELECT * FROM country_zones WHERE country='$user_country'")->result();
Let's say user_country is Canada, then your query should be:
SELECT * FROM country_zones WHERE country LIKE '%"Canada"%';
So, try this:
$country = $this->db->query("SELECT * FROM country_zones WHERE country LIKE '%\"$user_country\"%'")->result();
Edit:
LIKE query is needed because column contains ["canada","United States"] and you want to match it with Canada. So above LIKE query is SQL way of saying give me rows where country contains "Canada".
See: https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
Related
I want to fetch the one word out of the 4 words in the column category. For example I search for Buffet and the restaurant that has Buffet will display. This is my code so far and unfortunately it doesn't work.
HomeController
public function searchresto(){
$searchinfo = $_POST['searchinfo'];
$this->load->model('RestoModel');
$restaurantinfo['restaurantinfo']=$this->RestoModel>searchRestaurant($searchinfo);
$this->load->view('pages/searchDisplay',$restaurantinfo);
}
RestoModel
public function searchRestaurant($searchinfo){
$sql = "SELECT * FROM restaurants WHERE restoname = '$searchinfo' OR restocuisines = '$searchinfo' OR category = '$searchinfo'";
$result = $this->db->query($sql);
$result = $result->result('array');
return $result;
}
First, sanitize user input. Never query the database directly from the user input, as this may cause SQL Injection.
After sanatizing the user input, try using the LIKE function.
For example:
SELECT 'Breakfast, Lunch, Dinner, Buffet, Snack' LIKE '%Lunch%' would output 1.
SELECT 'Breakfast, Lunch, Dinner, Buffet, Snack' LIKE '%NonExistantCategory%' would output 0.
Try changing your query to SELECT * FROM restaurants WHERE category LIKE '%$searchinfoSanatized%', where $searchinfoSanatized is the input that has been filtered/escaped.
Also, I believe you are forgetting a - after RestoModel: $restaurantinfo['restaurantinfo']=$this->RestoModel>searchRestaurant($searchinfo);
Hello I have 2 textboxes and i want to give to the user the option to choose one in order to find results. The user can search through the id or the name. My problem is because i use LIKE%field% when the user chooses to search through the id the name field stays empty and returns all the table rows. I want to have results only if the user enters some value in the textbox. This is my sql query. I'm using mysql
"SELECT * FROM properties WHERE ID='$id' OR Name LIKE '%$name%'"
Thank you all
If the user has to select which field to search, you can do:
if ($_POST['search'] == 'id') {
$sql = "SELECT * FROM properties WHERE ID='$id'"
} else {
$sql = "SELECT * FROM properties WHERE Name LIKE '%$name%'"
}
You can do this in a single query (values are checked from the query itself):
"SELECT * FROM properties WHERE ('$id'='' OR ID='$id') AND ('$name' ='' OR Name LIKE '%$name%')"
Explanation:
First condition:
The query will select records with ID='$id' only when $id is not empty.
If $id is empty, query will not go for the second part ID='$id'
Second condition:
The query filters records with Name LIKE '%$name%' only when $name is not empty.
If $name is empty, query will not go for Name LIKE '%$name%'.
NB: This technique is extremely useful when you have numerous parameters to check, rather than using a bunch of if...elses at php side.
I have a query in SQL (Mysql) using a where clause.
SELECT * FROM TABLE WHERE name = 'Bristols';
Now I know that there's a row in the table containing Bristol's with an apostrophe, but not one without an apostrophe. However I want to return the row anyway. The problem is that I can only feed the query a value without an apostrophe: Bristols - is there any way within the query to remove the apostrophe from the field the query is searching?
SELECT * FROM TABLE
WHERE replace(name, '''', '') = 'Bristols'
There are several ways to accomplish this:
See Fiddle
Regex:
SELECT *
FROM cities
WHERE name REGEXP 'Bristol\'?s';
Replace:
SELECT *
FROM cities
WHERE 'Bristols' = replace(name,'\'','');
Explicit Matching:
SELECT *
FROM cities
WHERE name IN('Bristols','Bristol''s');
You have Two possible outlooks:
First:
SELECT * FROM TABLE WHERE name LIKE '%Bristol%' // Gather data like: BrISTOLS, Bristols, Bristol, Bristol's,
Second:
SELECT * FROM TABLE WHERE replace(name,'''','') = 'Bristols'
I am having a Problem with a MySQL Query. My Database Structure looks like this:
id (PRIMARY_KEY, AUTO_INCREMENT)
deviceID (TEXT)
name (TEXT)
latidude (TEXT)
longitude (TEXT)
Now this is how the first Entry looks like (in the order like above):
1 fc29daf8-bc53-4235-a1df-7d54b4e67b4c username 46.993393 5.448076
I'm search the Database with this Query:
$result = mysql_query("SELECT name FROM position WHERE deviceID = '$deviceID'");
But it doesn't find the entry. The deviceID is obtained from $_GET and it is exactly the same as this in the database. I also checked if it is right by printing it with echo.
It is sent to the PHP file like this:
read_uniqueid.php?deviceID=fc29daf8-bc53-4235-a1df-7d54b4e67b4c
and read from the .php file like this:
$deviceID = $_GET['deviceID'];
echo $deviceID.'<br>';
After those lines the query from above is sent.
There is a working connection to the database.I only have a Problem in the query if I search the name like that it works then I get all the other Entries.
This query works and gives me all entries:
$result = mysql_query("SELECT * FROM position WHERE name = '$name'");
Try like,
$result = mysql_query("SELECT name FROM position WHERE deviceID like '%$deviceID%'");
the column deviceID may have space in before or after the value.
I have a jQuery list which is returning a list of user_name on php page like
rohit,Bhalu,Ram
Now I want to filter the user_names from the database which is not the part of above list
So far I am trying the basic query of mysql like
select * from table_name where user_name NOTIN('rohit','Bhalu','Ram');
But problem with above query is, this a not the specific solution for bigger list which contains 1000 user_name so I want to use some query filter with php
Please suggest me what should I do in this stage ?
First use index for field user_name.
Second use this query (in $array - usernames)
$array = array('Rohit', 'Bhalu');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
$query = "select * from table_name where user_name NOT IN($comma_separated)";