通常網頁要連到資料庫都是使用localhost
這樣只有網頁在伺服器內才可以連到資料庫
如果網頁是在別台伺服器上就要允許外連資料庫的權限
允許方式有很多種,這邊只介紹這個方式
修改Mysql的設定文件
#sudo vim /etc/mysql/my.conf
找到 bind-address = 127.0.0.1
將這行設定註解掉就可以讓外部伺服器連入MySql
lastInsertId()可以抓到最新的ID$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
$dbh = new PDO($dsn, $user, $password);
$stmt_artist = $dbh->prepare(
'INSERT INTO `table_artist` (artistName) VALUES (?)'
);
$stmt_year = $dbh->prepare(
'INSERT INTO `table_year` (albumYear) VALUES (?)'
);
$stmt_year->execute(array( $year ));
$year_id = $dbh->lastInsertId();
$stmt_link->execute(array( $artist_id, $album_id, $genre_id, $year_id ));
INSERT INTO user (password) VALUES (SHA1($str));$ sudo mysql --defaults-extra-file=/etc/mysql/debian.cnf mysql> use mysql mysql> update user set password=password('abcdef') where user='root'; mysql> flush privileges;
mysql> quit