I have a column in table which has possible values of tom,john,jerry(SET). I have a variable that a user enter and I store it in $var and want that at some point. I could check that if the value in $var exists in particular row then don't update else update, How I can do this in PHP.
I have several rows like tom,john
john,jerry
I dont want tom,john,tom
I have one suggestion.
Please change the database values so that you have: ,tom,john,tom, (Notice the appending and preceding commas ,.
Now if you want to search for tom, fire an SQL like:
SELECT id FROM DB_TBL WHERE field LIKE '%,$val,%'
Repeat this for all values user has entered OR put in a single text box with commas.
Hope this works.
Related
I have a list of names in my database and I have to find the id of the name that I pass in the url.
My problem is that the names I pass in the url will not have a space in them while the saved record will in the database. When I search the database I get no records found.
e.g database record is "My Name" while what I will be passing in the url and searching with is "myname"
if(isset($_GET["name"])) $name = $_GET["name"];
SELECT id
FROM table
WHERE name Like '%$name%'
Thanks for any help.
// id don't know the exact syntax, but this is what you are searching for I guess:
// replace spaces with nothin temporarily, then check equal (=) not like (%%) if name is exactly the same (except the spaces)
SELECT id, REPLACE(name, ' ', '') AS tmp FROM table WHERE tmp='%$name%'
Obviously not the right way to store or search for, but if you must, try replacing the spaces with blanks like this:
SELECT id
FROM table
WHERE REPLACE(`name`,' ','') LIKE '%$name%'
I think best practice is to store another column, called something like name_nospaces and insert myname as a calculated value into it when you insert the My Name record. You could also create a view that contains myname as a column and query for it, but the advantage of another column in the original table is that it can be indexed for fast retrieval.
Im having some troubles showing the unique values from my database.
As an example, I have this very simple test table:
Where you can see that there is a duplicated value on the Tags column, named intro.
I would like to echo all of the UNIQUE tags, and I've tried with the DISTINCT command, but I might be doing something wrong.
This is my actual query:
SELECT DISTINCT tags FROM blog
But this gives me ALL of the tags.
Any help would be appreciated, thanks.
According to the image there are just to rows in the table, one contains "intro" and the other contains "intro, php, warning, errors".
If so, there are not the same.
I think you try to use a row for each value.
To do it you should insert each value in a separated sentence, or in the same but using different rows, something like:
insert into blog(tags) values ("intro"),values ("intro"),values ("php")
This probably has a simple solution, although it is made a little more difficult because of the way the database is constructed, it isn't mine. A column in the database has a text value that is of the form text1DDtext2DDtext3, where DD is a delimiter that they through in rather than having a separate table for 0 to n values that go in that column.
There is a search that is executed where what I have to start with is:
"text1","text2", "text3", . . .
All I want to do is build on a query that checks to see if any of the "textn" strings are in the column field, although it would be nice to have a query that also checked to see if all of the search string text values are in the column value. The order in which they are stored in the column can vary, as can the search string. If there was a linked table that just had single values in a column it would not be very hard.
I've just various combinations of IN and LIKE, and that doesn't seem to work.
Thanks.
try:
SELECT columnYouWant FROM dbo.table WHERE UPPER(column) LIKE ('%TEXT%');
I'm new to SQL and PHP. Maybe I don't understand what you will answer, so please explain it fully that I can understand so here's my question
I have 5 columns: Id, Name, UserName, Password, valuie
You can understand 4 columns, the fifth is valuie means what the user want to give in wish list. Now when user select a thing and add into wish list, that's good but when he/she adds 2 things in his wish list, how to put that in valuie? how can I display 1st and 2nd value? I mean if I want to display 1st one and if 2nd and if both, what I can do about it?
My PHP is good but MySQL is not good....
Code
Insert into user(Name, UserName, Password, Valuie)
Values("bla bla", "blabla", "blabla", "here's 1st value", "here's second");
This seems like something that should be handled by a one-to-many relationship. A user can have many items in his wishlist. This means that you will need to split your current table up into two. Example: A user table and a wish list table:
user: id, name, etc.
wishlist: id, item_name, user_id
Whenever the user adds a new wish list item, it should be added to the wishlist table, keyed by his/her user_id.
Seeing as you are new to MySQL, you should make sure that you read up on the concept of database normalization.
you can use explode function for it, here is a example below, which will make you easier to understand..
suppose you have a field in your db as value=(value1,value2)
now you can fetch both of the these values one by one as following..
$data=explode(',',value);
//An Explode function gives you an array,
//by which you can get any desired value just by passing it's index.
$data1=$data[0];
$data2=$data[1];
Hopefully this would help you.
Thanks.... :)
For that you can use simple technique. Create two columns Value1 and Value2. If user select first value then store it in Value1 and when user select second the store this value in Value2. Before performing this check if the Value1 field is Null in Database. If not then put that value to Value2.
You can also display both values on different location, if you want.
Say if I have an array and I want to check if an element is a part of that array, I can go ahead and use in_array( needle, haystack ) to determine the results. I am trying to see the PHP equivalent of this for my purpose. Now you might have an instant answer for me and you might be tempted to say "Use IN". Yes, I can use IN, but that's not fetching the desired results. Let me explain with an example:
I have a column called "pets" in DB table. For a record, it has a value: Cat, dog, Camel
(Yes, the column data is a comma separated value). Consider that this row has an id of 1.
Now I have a form where I can enter the value in the form input and use that value check against the value in the DB. So say I enter the following comma separated value in the form input: CAT, camel
(yes, CAT is uppercase & intentional as some users tend to enter it that way).
Now when I enter the above info in the form input and submit, I can collect the POST'ed info and use the following query:
$search = $_POST['pets'];
$sql = "SELECT id FROM table WHERE pets IN ('$search') ";
The above query is not fetching me the row that already exists in the DB (remember the record which has Cat, dog, Camel as the value for the pets column?). I am trying to get the records to act as a superset and the values from the form input as subsets. So in this case I am expecting the id value to show up as the values exist in the column, but this is not happending.
Now say if I enter just CAT as the form input and perform the search, it should show me the ID 1 row.
Now say if I enter just camel, cAT as the form input and perform the search, it should show me the ID 1 row.
How can I achieve the above?
Thank you.
The function you're looking for is find_in_set:
select * from ... where find_in_set($word, pets)
for multi-word queries you'll need to test each word and AND (or OR) the tests:
where find_in_set($word1, pets) AND find_in_set($word2, pets) etc
IN() Check whether a value is within a set of values
mysql> SELECT 2 IN (0,3,5,7);
-> 0
mysql> SELECT 'wefwf' IN ('wee','wefwf','weg');
-> 1
SELECT val1 FROM tbl1 WHERE val1 IN (1,2,'a');
View: IN MySql
I've got several things for you in terms of feedback & in direct response to your questions:
First, I suggest you sanitize the input. Everybody is going to tell you that. For that, see What’s the best method for sanitizing user input with PHP?.
Second, normalize the input with UPPER() or LOWER() if you want to use MySQL and need to store user-formatted input, or use strtoupper() and strtolower() if you wanted to process the input before storing it.
You're still left with the order in the user query. E.g. "cat, dog" ought to yield the same result as "dog, cat". If you were to code that with a LIKE statement, performance issues are going to eat you alive. Not only would you have to create the query dynamically, you'd also end up with huge and unnecessarily complex queries. In short, forget it. You have to change the way you store your data.
One way to accomplish this is by creating a relationship table that references a table of unique user input and your record. This table would look similar to
user_id | pet_id
Every user could have more than one pet_id associated with them. I've set up a database a long time ago the same way you did and ran into the same issues. Performance-wise it never paid off and it's anything but good style. I ended up changing my structure because of that to the above-mentioned method.
This mysql function search an INT value into a json array of INT:
-- EXAMPLES :
-- select is_into_json_array(18, '[25, 10, 15]'); -> -100
-- select is_into_json_array(25, '[25, 10, 15]'); -> 0
-- select is_into_json_array(15, '[25, 10, 15]'); -> 2
https://github.com/PietroLaGrotta/Json-in-mysql-text-type/blob/master/is_into_json_in_array.sql
Yes, the column data is a comma
separated value
Here is your fault.
No, it shouldn't be comma separated value
And your database structure should be normalized.