数据库 - Database

Added: 2022-03-12 16:51:16 | Last: 2022-03-19 13:54:08

I have been using all these 4 databases:
1. MySQL for my personal data including this website. It's totally free.
2. Oracle for company business
3. MongoDB for documents
4. Microsoft SQL server for Bosch BIS and BVMS
Here want to share how to use MySQL with PHP on ubuntu Linux server.
# Install MySQL
> sudo apt-get -y install mysql-server
# change mysql root password:
> mysql
> mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxxxx'
# Create a new DB:
mysql > create database DB_NAME;
# Export an existing DB from Linux OS level:
> mysqldump -u root -p DB_NAME -h DBHOST >db.sql
# Copy table structure to a new table
mysql > CREATE TABLE new_table LIKE old_table;
# Import a DB:
> mysql -u root -p DB_NAME -h db_hostname < db.sql
# Export a table to csv file:
mysql > SELECT * from table_name INTO OUTFILE '/var/lib/mysql-files/mysql.csv' ;
# Install myphp-sql so that php can access DB
>sudo apt-get -y install php libapache2-mod-php php-mysql php-curl php-gd
# PHP accessing MySQL DB using OO coding
$db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($db->connect_errno){
printf("Connect failed: %s\n", $db->connect_error);
exit;
}
$sql="select * from Table_name";
if($result = $db->query($sql)){
while($row = $result->fetch_assoc()){
Your_Actions;
}
}