Update a mysql table replacing it

Replacing a table with the same name, I mean. You have to add, before “CREATE TABLE tablename” this row:

DROP TABLE tablename;

You can also add single rows, exporting them, and importing in a sql query, like the following:

INSERT INTO bibliografie (ID, autore, autore_nome, titolo, imagelink, sigla, tipologia, curatori, curatore_unico, opera, rivista, num, pagg, edizione, luogo, data, data_spec, trad_titolo, trad_edizione, trad_luogo, trad_data, contenuti, keywords, reperibilita, letto, letto_quando, valutazione, riferito, destinazione, ambito, href, ad_code) VALUES
(1880, 'Godechot', 'Jacques', 'La Révolution française : chronologie commentée, suivie de notices biographiques sur les personnages cités', NULL, 'Godechot [1988]', 'libro', NULL, NULL, NULL, NULL, NULL, NULL, 'Perrin', 'Paris', '1988', NULL, 'La Rivoluzione francese. Cronologia commentata 1787-1799', 'Bompiani', 'Milano', '2001', '', 'Rivoluzione francese ', NULL, NULL, NULL, NULL, NULL, NULL, 'storia', NULL, NULL);

leaving xampp

For many years I used xampp (for Linux: Lampp), because of its simplicity, unlike the “native” Linux apache/php/mysql apps, wich seemed to me very difficult to configure.

The pro of xampp is its simplicity (with few mouse clicks you can do all to install and configure your local php/mysql sever). But the con is that your xampp apps don’t are updated regularly, and there can raise conflicts with Linux “native” mysql.

Therefore I decided to learn how to configure “native” (system-rooted, so to say) apache/php/mysql apps. I chose to install mariadb and not mysql.

And in these last weeks I managed to configure the server, with the following steps:

  • sudo apt install -y apache2
  • sudo apt install -y php
  • sudo apt install mariadb*
  • sudo mysql_secure_installation
  • sudo mysql -u root -p (and within >mysql shell
    • use mysql
    • GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' WITH GRANT OPTION;
    • SHOW GRANTS FOR 'root'@localhost; //to check if it is all right
    • FLUSH PRIVILEGES;
    • exit;
  • sudo systemctl enable mariadb.service

mysql add some string to several rows

If a column has several rows, i.g.:

'red'  
'NULL'  
'red, yellow'  
'green'  
'NULL'  
'yellow, green'

And you want to add a new string (‘blue’) to every row, so that the result be:

'red, blue'  
'blue'  
'red, yellow, blue'  
'green, blue'  
'blue'  
'yellow, green, blue'

you can use this code

UPDATE my_table 
SET mycolumn = CONCAT('same new string ',mycolumn); 

But the above code works only for rows with some content, not for the empty (NULL) ones. Therefore you can complete your task with the following:

UPDATE my_table 
SET mycolumn = 'same new string' 
WHERE mycolumn IS NULL; 

mysql/php output in utf8

To avoid output problems in non-latin characters you should add this line in a msyqli query (i.e. after $db = mysqli_connect($db_host, $db_user, $db_password, $db_name);):

mysqli_set_charset($db, 'utf8');

import an external table to a mysql table

  • You can use a graphical interface to prepare the rows to insert in mysql table: be careful that a) the number of columns be matching with the mysql table rows number and b) the order of columns be the same.
  • Save your date as csv: in this way you will be able to make further modifications to your data: i.g. you could add new ,NULL,columns (because in librecalc if a column is empty at the end of a file that column is not added).
  • Be very careful with comma (,) within the columns, if you import the table in mysql with comma as column separator. You should avoid commas within a column, if you use commas as separator.
  • In phpmyadmin import your csv file as a new table. As and of line you can set \n if yout line ends with something like “NULL”. Otherwise, if you rows ends with “NULL”, you will get an undesired column at the end of teh impoorted table.
  • Don’t worry if the columns type doesn’t match with the destination mysql table: this is negligible.
  • In sql do this command:

INSERT IGNORE
INTO your_destination_table
SELECT *
FROM the_new_data_table-from-csv;

  • Using insert IGNORE you can avoid to delete the ID beginning column, if it is simply an autoincrement one.
  • Your new rows should have been added to your destination table: done!

How to hide an empty element

with css

To hide an element you can use css: element:empty (such as p:empty) {display:none}, or img[src=""] {display:none}.

with mysql code

You can, even with PDO, use this syntax

if ($data[0]['somecolumn'] != '') {echo " " . $data[0]['somecolumn'] . "";}

regex multiple replacements

You could have to replace several “whatever”, f.e. in a sql file I had to replace many columns with dates, such as ‘2005-08-15’ splitting them in ‘2005’, ’08’, ’15’.

I manage to do it with this code:

find NULL, '(.*?)-(.*?)-(.*?)', NULL, NULL, NULL
replace NULL, '\1', '\2', '\3', NULL

In this way you can replace '2005-08-15', NULL, NULL, NULL with '2005', '08', '15', NULL, and likewise all the like, such as '2012-06-23', NULL, NULL, NULL with '2012', '06', '23', NULL and so on.

But, be careful: make a backup of your file before.

another case: if you want replace several digits with the same digits formatted, you can try something like this code:

search: ^    (\d{2})$
replace: <h2>cap. \1</h2>\n