code:
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', '', '$string') WHERE something = 'something';
In this query I have an variable $string where I want when I click on submit button it store data like ,a,b,c,d but now what happen when I click on submit button it update my table with ,a and doing same process at second time it update ,b but I want ,a,b so how can I do this? Please help me.
Thank You
You were almost there !
If you want to append a new string to your field here is the query :
$insert = "UPDATE TABLE SET FIELD = CONCAT_WS(',', FIELD , '$string') WHERE something = 'something';
Because if you look how work
CONCAT_WS (W3school link)
it's CONCAT_WS(separator, expression1, expression2, expression3,...)
here expression1 is the FIELD you want to update and expression2 the $string
Related
I am trying to update mysql table MYTABLE using two value. One is STAR column which should be incremented by one on each query, and the second one is COMMENT column which should be concatenated with existing one on each time and separated by comma.
Below is the command I used, but not working.
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, ','.$comment) where ID='$id'";
$query = "update MYTABLE set STAR=STAR+1,COMMENT = CONCAT(COMMENT, ',', '$comment') where ID=$id";
where ID='$id'
is incorrect because $id might be a number, so, delete the "'".
Have you escaped the $comment variable ?
Otherwise you may use prepared statements with PDO :)
I hope you're using PDO...
you should but string in '' and update your query, it has error syntax :
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, '$comment') where ID='$id'";
To make it more secure, just use following code...
$query = "update MYTABLE
set `STAR` = `STAR`+1,
`COMMENT`= CONCAT(COMMENT, '$comment')
where `ID`='$id'";
Happy Coding...
I have this query, which spits out that I have an error in syntax. I cannot for the life of me understand what it is. I have a table, where one column is email and the other is subscribed (the latter of which is a boolean using tinyint). Any idea what's wrong with this syntax?
$query = "UPDATE $DB_TABLE SET $DB_IS_SUBSCRIBED_KEY = 0 WHERE $DB_EMAIL_KEY = $email";
Your email value needs to be wrapped in quotes.
UPDATE tablename SET columname = 1 WHERE emailcolumn = "email#email.com"
I have simple query:
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT DATE(date_time) AS day_h
FROM `$table_name`
");
and it is working ok.
But I must do this query with table name and when I try this:
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT `$table_name`.DATE(date_time) AS day_h
FROM `$table_name`
");
or
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT v_c_holi_2012.DATE(date_time) AS day_h
FROM `$table_name`
");
this is not working (Fatal error: Call to a member function fetch_assoc() on a non-object).
What I 'm doing wrong?
The date function should not have the table prefix since it is a system function.
Instead you need to put the table alias before your field date($table_name.date_time).
By the way, you don't need to if you select from only one table.
I believe $table_name.DATE(date_time) should be DATE($table_name.date_time)
You should apply the table name to the thing that's in the table (i.e. the field name), not the DATE function (which has nothing to do with your table).
i.e.
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query(
"SELECT DATE(`$table_name`.`date_time`) AS `day_h`
FROM `$table_name`"
);
Here I want to search like
I have a string say $var = "1,3,5,7"
And
in my table say table student there is a column say column_abc
And this column contain values like
column_abc
2,3,45,6
1,3,4,5,8
3,4,6,9
1,5,10,13,34
I want to search $var against that column column_abc
can anybody help me
In result I want rows which contain any number present in $var
Am I right that you want to search in the table students for the value of column_abc? In that case you can use the following PHP code:
$result = mysql_query("SELECT * FROM `student` WHERE `column_abc` = '" . mysql_real_escape_string($var) . "'");
Hope this helped.
This solution may work for you ...
where column_abc regexp '(^|,)$var($|,)'
If $var is equal to 2, then it will find it twice in the below scenario:
2,3,45,6,12
1,2,3,4,5,8
3,4,6,9
1,5,10,13,34
EDIT: Just re-read your question, and you were looking for an entire string of numbers, and not just one ....
declare #search int(10)
declare #Search2 int(10)
Set #Search = '1,2,3,4'
Set #Search2 = #Search+'%'
select * from student where _abc like #Search2
I have a table with 27 varchar fields. I want to make all fields lowercase, but i want to do it in one short mysql call.
This does a single field:
UPDATE table
SET field = LOWER(field)
How do I do the equivalent of this (which doesn't work):
UPDATE table
SET * = LOWER(*)
You can't do it with your creative attempt SET * = LOWER(*) etc.
You can however do it like this:
UPDATE table SET
column1 = LOWER(column1),
column2 = LOWER(column2),
-- etc, listing all text type columns
columnN = LOWER(columnN);
The reason there's no "shortcut" is probably because this pattern is so infrequently needed.
The consensus is that this cannot be done in a single mysql query.
Here is a super quick PHP script that does this for N fields (thanks for the idea #alex):
$sql = "SHOW COLUMNS
FROM table";
$results = mysqli_query($dbcon,$sql);
while($column = mysqli_fetch_assoc($results))
{
$column = $column["Field"];
$sql = "UPDATE table
SET $column = LOWER($column)";
$success = mysqli_query($dbcon,$sql);
}