【问题标题】:How to use PHP in HTML for mySQL如何在 HTML 中为 mySQL 使用 PHP
【发布时间】:2017-04-26 11:17:13
【问题描述】:

我第一次尝试使用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

【问题讨论】:

  • 那么问题是如何使用 PHP 运行查询?您应该为此使用 PDO 或 mysqli。如果接受输入,则应将查询参数化。
  • @chris85 我不知道那些是什么!是否有更简单的方法或示例如何从 MySQL 读取列?
  • 你能把它缩小到更小的东西吗?这段代码中包含视频、FTP、HTML、PHP、Python、MySQL 和其他可能的东西。
  • @tadman 对混乱的代码感到抱歉,您也许可以忽略所有这些并考虑一个简单的新 html 文件,其中没有任何内容。我想要的只是能够从 MySQL 中检索数据。
  • 这非常接近于请求教程,这是题外话。如果您不知道从哪里开始,我会看一下 Laravel 之类的东西。它会为您指明正确的方向。

标签: php python html mysql raspberry-pi


【解决方案1】:

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();
    }
?>

【讨论】:

    【解决方案2】:

    您有多种选择。最简单的,恕我直言,如下所示。

    将您的 index.html 文件重命名为 index.php(确保您的网络服务器查找 index.php 以及 index.html 和 index.htm)。

    然后您的 index.php 将被修改为如下所示:

    <?php
    //Open MySQL connection
    //Get the data you want into a string variable called $info
    print <<< END
    <!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>
    ... (rest of HTML excluded for brevity)
    END;
    ?>
    

    只需将 $info 变量放在您希望数据出现在 HTML 数据中的任何位置。

    构造:

    print <<< END
    <<<what you want to print>>>
    END;
    

    被称为“heredoc”,非常好用。

    我没有描述如何实际从数据库中获取数据,因为您的问题似乎更多地是关于使用 PHP 输出带有数据库数据的 HTML 的过程。查看 PDO 文档 (PDO documentation) 以快速了解 PHP 中的数据库访问。

    【讨论】:

    • 当您可以使用 php 标签 (?&gt;&lt;html&gt;&lt;?php ) 时,为什么还要使用 heredoc?另外,这和mysql有什么关系?
    • @t.m.adam 我喜欢这种格式,因为它更易于阅读和维护。在整个代码中散布一些短标签是丑陋的,而且更难看出发生了什么。在这种情况下,只有两个地方需要使用短标签。这是个人喜好。如果您查看我的答案,我确实专门回答了 MySQL 详细编码答案。任何数量的 PHP/MySQL 编码教程都很容易回答这个问题。正如我所说,我认为这个问题更多的是如何使用 PHP 输出 HTML(或创建与他的 index.html 文件等效的文件)。
    • 好的,我明白了。我使用标签,或者只是在字符串中嵌入变量,但正如你所说,这是个人喜好问题
    • @mlewis54 感谢您的回复,但我的问题是我不知道我的服务器(WebIOPi)是否允许我运行 php!我确定如何引导它使用 index.html 并运行它。我正在使用带有 php 插件的树莓派 3 获得了 MySQL。不需要多个 index.php 和其他文件,直接将 php 代码放入 index.html 是不可能的吗?
    • 我相信有一些问题可以让您将 php 作为涉及 .htaccess 文件规则的 html 文件运行。我不熟悉 Webliopi 作为我所读过的 Web 服务器。因为它看起来非常面向 Python,所以我没有过多关注它。我建议使用 NGINX 甚至 apache。我在 Raspberry PI 3 上使用 NGINX 设置了几台服务器,它们运行速度很快,并且可以支持大量连接。有许多很棒的功能,例如反向代理。设置起来相当容易。话虽如此,您可以使用 PHP 程序在 Google 上运行您的 Web 服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2021-11-07
    • 2015-06-02
    • 2018-08-22
    相关资源
    最近更新 更多