I have saved one data column as bit(2) in my sql table. column name = state2
when i checked the sql table using cpanel I can see the numbers there. I store 0-3 values there so I selected 2 bit as column type.
my sql column updates correctly. but now I am trying to use that value in my if condition. but if condition does not work with that, then I tried to echo the value in each row under column state2.
It prints empty string. why is that ? is there any special method to use with bit(2) data types.
check screenshot for value in sql table
Now this is what I am doing
$state2 = $row['state2'];
echo '<td>'.$state2.'</td>';
I m not attaching full code because it is very long. but I hope this part is enough. I have setup it correctly because it prints other all values correctly in my html page as table row.
look below image and you will see results
what is the reason for this. below image show the data type of state2 column.
please help
I found the reason my self. it was my fault,
I have not added state2 in select statement in sql
I added it and it worked perfectly
https://prnt.sc/u0big4
Related
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 am trying to something in MySQL that I do in JavaScript/PHP all the time. I need to concatenate the value of a field in MySQL with a value that is passed into a query from PHP. For example, let's say I have a field called favourites with a value of 27 and I have this query:
UPDATE useraccs SET favourites = favourites + ',30' WHERE id='10'
My desired new value for favourites would be 27,30, but I'm getting 57, where clearly SQL is adding them numerically. I have set the data type for this column as TEXT and was hoping that would force SQL to treat it as a string all the time, but that doesn't seem to be the case.
In my research I read about the CONCAT() function, and I tried this:
UPDATE useraccs SET favourites = CONCAT(favourites,',30') WHERE id='10'
That results in a failed query. The logic feels right but that is obviously not how that function is meant to be used.
I acknowledge that in theory, I could just grab the original value of favourites and concatenate it with the new value in the PHP itself and then send it to MySQL, but I feel like there MUST be a way to do this in one query...if I'm wrong about that so be it, but I'm sure there must be a way.
Use the following to create '27,30':
CONVERT(favourites,char) + ',30'
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.
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.