MySQL query doesn't find result when searching a String - php

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.

Related

Check which columns were modified in an UPDATE query

When we update a MySQL record with php, we can check if it has effect using:
$mysqli->affected_rows;
But how do I check which column has been modified?
Example, in my table have the columns: id / name / age
In a record we have the data: 1 / Woton / 18
If I send an: UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'
Only the age field has changed. How can I determine this?
You cannot directly get the updated columns from the query result.
It can be get from some php query. Firstly we will have to select the row from database which we are going to update in a array variable. Than run the update query for the same row.
Lastly get the same row from database from select query in the new array variable.
Finally we get two arrays.
We can get the updated column with the array_diff_assoc php function.
See the below code for the same.
$sql = "SELECT * from mytable where id=1 limit 1";
$prev = mysqli_fetch_assoc(mysqli_query($conn, $sql));
//Get the column data in the array. Before update.
$sql = "UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'";
$conn->query($sql);
// Update data
$sql = "SELECT * from mytable where id=1 limit 1";
$updated = mysqli_fetch_assoc(mysqli_query($conn, $sql));
// Again run the select command to get updated data.
$UpdatedColumns=array_diff_assoc($updated,$prev);
In a different note: If QueryLog has been enabled in the DB then you (or your script in PHP or Python or any) can easily see/read which part of the content has been updated and even you can monitor the DB.
The good news is, even you can target which table, which query etc.

MySQL Select column name include variable PHP

I want to transfer data from a MySQL database, select the column related to the language of values from my goal.
MySQL table's name is catalogs
Mysql column names are id, name_en, name_de, name_ru, content_en, content_de, content_ru, image
language short code comes from _GET request
For example, the URL looks like the following
catalogs.php?id=1&lan=en
My PHP Code Looks like the following
$get_id = $_GET['id'];
$lan = $_GET['lan'];
$sql = $mysqli -> query("SELECT id, name_$lan, content_$lan, image FROM catalogs WHERE id='$get_id'");
while($row = $sql->fetch_array()){
echo $row['name'];
//nothing display
}

MySQL select query comma separated numbers filter result

My table look like this:
Table screenshot
Here I'm getting the result by query:
$subject_ids = implode(',', $_POST['subject_ids'])
SELECT * FROM table WHERE focusarea LIKE '%$subject_ids%' ;
The result is perfect, but there is nothing to display when I select more than one subject ids, like if selecting only one then it shows,
but when to select 1, 2, and 4, but there is nothing with this LIKE query...
How can I fix this?
Use implode like,
PHP
$subject_id_aray = explode(",",$_POST['subject_ids']);
$in_array_string = array();
foreach($subject_id_aray as $values){
$in_array_string[] = "'".$values."'";
}
MySql
$sql = "SELECT * FROM table WHERE focusarea in (".implode(",",$in_array_string).") ;";
LIKE clause will not work in your case because using LIKE '%1,2,3%' in query will not get anything, as you as using Ids you should use IN instead of LIKE. LIKE will be used separately for each id if it is string.
As you are getting $_POST['subject_ids'] as an array, query will be like
$subject_str = implode(',', $_POST['subject_ids']);
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
If your column focusarea is not integer then
$subject_str = "'".implode("','", $_POST['subject_ids'])."'";
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
Maybe you have bug in POST.
Try to echo, $subject_ids befor inject to SQL.
You focus are is simple string of numbers, connected by ,, but what you are sending by POST maybe is not correct.
Other problem, this don't look like you full code.
Provide you file, if this don't resolve problem.

Extract SQL Query with JSON - Codeigniter

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

WHERE clause doesn't seem to work in PHP

The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.

Categories