PHP로 MariaDB 접속하기
- php와 mysql 연결 모듈 설치
- $ sudo apt install php-mysql
1234567891011121314151617181920212223242526272829303132333435363738pi@raspberrypi:/var/www/html $ sudo apt install php-mysqlReading package lists... DoneBuilding dependency treeReading state information... DoneThe following packages were automatically installed and are no longer required:alsa-base gstreamer0.10-alsa gstreamer0.10-plugins-baselibgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libxfce4util-binlibxfce4util-common libxfce4util7 libxfconf-0-2 pimixer point-rpi vlc-l10nvlc-plugin-notify vlc-plugin-samba vlc-plugin-video-splittervlc-plugin-visualization xfconfUse 'sudo apt autoremove' to remove them.The following additional packages will be installed:php7.3-mysqlThe following NEW packages will be installed:php-mysql php7.3-mysql0 upgraded, 2 newly installed, 0 to remove and 36 not upgraded.Need to get 102 kB of archives.After this operation, 368 kB of additional disk space will be used.Do you want to continue? [Y/n] yGet:1 http://ftp.kaist.ac.kr/raspbian/raspbian buster/main armhf php7.3-mysql armhf 7.3.14-1~deb10u1 [96.4 kB]Get:2 http://ftp.kaist.ac.kr/raspbian/raspbian buster/main armhf php-mysql all 2:7.3+69 [5,992 B]Fetched 102 kB in 1s (75.5 kB/s)Selecting previously unselected package php7.3-mysql.(Reading database ... 172482 files and directories currently installed.)Preparing to unpack .../php7.3-mysql_7.3.14-1~deb10u1_armhf.deb ...Unpacking php7.3-mysql (7.3.14-1~deb10u1) ...Selecting previously unselected package php-mysql.Preparing to unpack .../php-mysql_2%3a7.3+69_all.deb ...Unpacking php-mysql (2:7.3+69) ...Setting up php7.3-mysql (7.3.14-1~deb10u1) ...Creating config file /etc/php/7.3/mods-available/mysqlnd.ini with new versionCreating config file /etc/php/7.3/mods-available/mysqli.ini with new versionCreating config file /etc/php/7.3/mods-available/pdo_mysql.ini with new versionSetting up php-mysql (2:7.3+69) ...Processing triggers for php7.3-fpm (7.3.14-1~deb10u1) ...
- $ sudo apt install php-mysql
- nginx 재시작
- sudo /etc/init.d/nginx restart
- PHP 프로그램 db연결 코드(/var/www/html/dbconn.php)
sudo nano /var/www/html/dbconn.php
1234567<?php$connect = mysqli_connect("localhost", "smart", "123", "dustdb");//host, ID, PW, DBif(mysqli_connect_errno()){echo "MySQL 연결 실패 : ". mysqli_connect_error();}?> - PHP 프로그램 SELECT 테스트(/var/www/html/select.php)
sudo nano /var/www/html/select.php
123456789101112131415161718<?phpheader("Content-Type: text/html; charset=UTF-8");include "./dbconn.php";$sql = "SELECT * FROM testtable";$result = mysqli_query($connect, $sql);$runners = array();while($row = mysqli_fetch_array($result) ){echo $row['_id'].", ";echo $row['pos'].", ";echo $row['ppm']."<br>";}mysqli_close($connect);?>- 실행 : 웹브라우저에서 http://라즈베리IP주소/select.php
- PHP 프로그램 INSERT 폼 (/var/www/html/insert.html)
sudo nano /var/www/html/insert.html
123456789101112131415<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>INSERT EXAM</title></head><body><form action="insert_post.php" method="post">pos : <input type="text" name="pos"><br>ppm : <input type="text" name="ppm"><br><input type="submit"></form></body></html> - PHP 프로그램 INSERT PHP(/var/www/html/insert_post.php)
sudo nano /var/www/html/insert_post.php
12345678910111213141516171819<?phpheader("Content-Type: text/html; charset=UTF-8");// 헤더에 charset 언급해주고include "./dbconn.php"; // dconn.php 파일을 불러옴echo "test";$pos = $_POST['pos'];$ppm = $_POST['ppm'];$sql = "INSERT INTO testtable(pos, ppm) VALUES ('".$pos."',".$ppm.");";//echo $sql;$result = mysqli_query($connect, $sql);mysqli_close($connect);echo $result."저장되었습니다.";?>- 실행 : 웹브라우저에서 http://라즈베리IP주소/insert.html