php - 如何在 HTML 中为 mySQL 使用 PHP

标签 php python html mysql raspberry-pi

我第一次尝试使用php连接树莓派上的MySQL。服务器和 MySQL 都在 Pi 上运行,我想调整我的 index.html 文件以显示数据库中的一些值。通常情况下,我可以用 pyton 做到这一点,但我不知道是否可以用 php 做到这一点。这是我的index.html:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ziks</title>
<link href="CSS/style1.css" rel="stylesheet" type="text/css">
<style type="text/css">
body {
background-color: #FFFFFF;
background-image: url(Images/twe_background-1920x1080.jpg);
}
</style>
</head>

<body>
<div class="header">
<p><strong>Welcome to Ziks</strong></p>
</div>
<p>&nbsp;</p>
<div class="box">
<p> Wishing every one a happy life! </p>
<form method="GET" action="ftp://47.55.90.215:21">
<input type="submit" value="Click here to view Disk">
</form>
<body>
<html> 
<body> 
</body> 
</html>
</body>
<div class="image"></div>
<iframe src="http://free.timeanddate.com/clock/i5nb4b3g/n1127/szw160/szh160/hoc9b8578/hbw10/hfc754c29/cf100/hnc432f30/hcw2/fav0/fiv0/mqcfff/mqs4/mql25/mqw12/mqd78/mhcfff/mhs2/mhl5/mhw2/mhd78/hhcfff/hhs2/hhl50/hhw8/hmcfff/hms2/hml70/hmw8/hmr4/hscfff/hss3/hsl70/hsw3" frameborder="0" width="160" height="160"></iframe>
  <video width="400" controls>
<!--<source src="///smb://zikpc/f/test.mp4" type="video/mp4">-->
<!--<source src="///smb://zikpc/f/test.ogg" type="video/ogg">-->

<source src="1.mp4" type="video/mp4">
<source src="1.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>

</div>
</body>
</html>

这个index.html 文件工作正常。我想做的就是在其中编写一些 php 来连接到 MySQL。我当前在 python 中使用的方法是:

# External module imports
import time
import os
import datetime
import MySQLdb
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')

# Connect to mysql
db=MySQLdb.connect("localhost","zikmir","gforce","temp_database")
cursor=db.cursor()

while True:
  # Initialization
  sensor= "/sys/bus/w1/devices/28-011620ee98ee/w1_slave"
  # Open the file for sensor
  file = open(sensor) 
  # Read all of the text in the file. 
  text = file.read()
  # Close the file now that the text has been read. 
  file.close() 
  # Split the text with new lines (\n) and select the second line.
  second_line = text.split("\n")[1]  
  # Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
  temp_data = second_line.split(" ")[9]
  # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
  temp = float(temp_data[2:])
  # Put the decimal point in the right place and display it. 
  temp = temp / 1000
  # Display time
  t= datetime.datetime.now()
  print t,temp
  # Push data into mySQL
  sql = "INSERT INTO time_temp VALUES(now(),%s)"
  cursor.execute (sql,(temp,))
  db.commit()
  # Wait 5 seconds
  time.sleep(5)

我的问题是如何在index.html 文件中的php 中执行此操作?任何帮助,将不胜感激! This is my website

最佳答案

MySQLi 面向对象

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

MySQLi 程序

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

PDO

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

关于php - 如何在 HTML 中为 mySQL 使用 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624228/

相关文章:

python - 如何将转换应用于 pandas 数据框列表?

python - Selenium 发现隐藏元素

javascript - 使用 Bootstrap 在移动平台其他桌面导航栏时显示汉堡菜单

php - 如何在 Laravel 5.6 中转换 json 文件中的 json 数据

php - 从图像和 PDF 中提取单词 - Laravel

php - 计算要删除的帧以获得所需长度的延时摄影

html - 我的 CSS 不起作用,除了我网站中的一页

PHP多维数组删除部分重复项

python - 从网页中提取元关键字?

html - 是否可以设置一个 div 或 table 完全卡住?