how to remove spaces from a field in mySQL db? [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a database where i store my photos title, tags, and their path on sever some of the records have names like:
photo 1234.png. I need to make them like photo1234.png
Why can't I use a query like
UPDATE tblPhoto a
set a.photoLink = replace(a.photoLink , ' ', '')
where a.photoLink like '% %';
And which is the best way to rename them in Linux Server, can I use php ?

You don't need where clause
UPDATE tblPhoto SET photoLink = REPLACE(photoLink , ' ', '');
For replacing the file name on your Linux Server you can try to look in this answer.
https://stackoverflow.com/a/2709619/7921383

Use php method for example:
$old_name="Hello World";
echo str_replace(" ","",$old_name);
//output Helloworld

Related

Remove escape symbols in string using mysql command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a MySQL column with data looking like this,
I need to convert the column to the JSON format via convert(somecolumn,JSON). However, it seems that I first need to remove the escape symbols (e.g., \"). I did some search and found that mysql_real_escape_string will do the job (from this question).
But if I understand correctly, mysql_real_escape_string is a PHP command. Is there any native MySQL command that do similar thing as mysql_real_escape_string (something like convert(mysql_native_function(somecolumn),JSON))?
Use REPLACE. For harder things REGEXP_REPLACE.
SELECT REPLACE(somecolumn, '\"', '"')
SELECT REGEXP_REPLACE('"..."', '(^"|"$)', '')
The latter will unquote the entire string, as ^ is the start, and $ the end.
BTW I would actually correct all the data in the table once. (After a backup.)
The mysql library is old.. if you really need to use something like it - use mysqli
the mysql_real_escape_string is not as secure as you would think it to be, see this: https://security.stackexchange.com/questions/8028/does-mysql-escape-string-have-any-security-vulnerabilities-if-all-tables-using-l
That said you're much better off by not using any of them but using Php PDO and replacing something like:
$data = [
'name' => $name,
'surname' => $surname,
'sex' => $sex,
];
$sql = "INSERT INTO users (name, surname, sex) VALUES (:name, :surname, :sex)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);
it will take care of the 'escaping' problems for you.
more examples here: https://phpdelusions.net/pdo_examples/insert

Random Number in MySQL column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to add random number in one of my database column called likes which have about 5000 row in it. currently I have tried like below to get random number from 10 to 50.
$randomnumberlike = (rand(10,50));
but this is setting same number in all row...instead I want different number in row...anyone can please suggest how can I do it ?
Thanks
Try: UPDATE Table SET fieldName = ((rand() * 1000) % 40) + 10;
Because when you preset it in a variable, it'll be the same for all...
You're setting a variable:
$randomnumberlike = (rand(10,50));
If you're then using $randomnumberlike to add to the db then it will always be the same, its already be assigned (unless within a loop), you need to use (rand(10,50)) (or working similar) directly in the query like this:
UPDATE <table_name> SET <field_name> = ROUND((RAND() * (50-10))+10) WHERE ...;

How can I write the following query in CodeIgniter? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working on an app in CodeIgniter, but I am not able to write the following query using active records,
select price, count(*) from fake_apps where downloads > 20000 group by price;
How can I write this query using CodeIgniter's active records?
Thank You.
$this->db->select("price,count(*)",FALSE);
$this->db->from("fake_apps");
$this->db->where("downdoad>","20000");
$this->db->group_by("price);
$res = $this->db->get();
It would be like this:
$this->db->select("price, count(*)",FALSE);
$this->db->where("downdoad > ","20000");
$this->db->group_by("price");
$sql = $this->db->get("fake_apps");
echo '<pre>';
print_r($this->db->last_query());
echo '</pre>';
For FALSE in Codeigniter select:
$this->db->select("price, count(*)",FALSE);
Note:
If you set it to FALSE, CodeIgniter will not try to protect your field
or table names with backticks. This is useful if you need a compound
select statement.
Reference:
https://ellislab.com/codeigniter/user-guide/database/active_record.html

Mysql function trim doesn't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried trim function many times to remove white spaces on a table column but it didn't .
update table_name set column_name= TRIM(column_name)
any guide please.
Why don't you just do SELECT REPLACE(column_name, ' ', '') FROM table_name?
If you want to remove the white spaces from both left and right side, trim() should do the work for you.
You can check the length of the returned values as follows:
select trim(column_name), char_length(trim(column_name)) as length from table_name
See this SQLFiddle demo
This is working fine for me.
Try this,
update yourtablename set price = REPLACE( price, ' ', '');

Replace character between numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I converted phpbb2 forum to phpbb3.
But I have problem with phpbb2 posts links which remained in MySQL database.
phpbb2 posts links are eg.
/viewtopic.php?p=106352#106352
and phpbb3 are:
/viewtopic.php?p=106352#p106352
(there is letter p after #)
Current links from phpbb2 are not working, after convert,
so I need help replacing # between posts id (numbers) in MySQL DB.
I got a lot of links like:
/viewtopic.php?p=106352#106352
and I need to replace # with p ( add p at the end)
like:
/viewtopic.php?p=106352#p106352
I don't know much MySQL, so I stuck.
Please help
One possible approach:
UPDATE phpbb_posts
SET post_text = REPLACE(post_text, '#', '#p')
WHERE post_text REGEXP '^[^#]*p=[0-9]+#[0-9]+$'
WHERE clause is specified to prevent updating links that do not follow the format.
SQL Fiddle.
select replace('/viewtopic.php?p=106352#106352','#','#p')
update myTable
set myColumn = replace(myColumn, '#', '#p')
where someColumn = someThing