如何在HTML for mySQL中使用PHP
我第一次尝试使用php连接到raspberry pi上的MySQL。服务器和MySQL都在Pi上运行,我想调整index.html文件以显示数据库中的一些值。通常情况下,我可以用pyton来实现这一点,但我不知道是否可以用php来实现这一点。这是我的index.html:如何在HTML for mySQL中使用PHP,php,python,html,mysql,raspberry-pi,Php,Python,Html,Mysql,Raspberry Pi,我第一次尝试使用php连接到raspberry pi上的MySQL。服务器和MySQL都在Pi上运行,我想调整index.html文件以显示数据库中的一些值。通常情况下,我可以用pyton来实现这一点,但我不知道是否可以用php来实现这一点。这是我的index.html: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Ziks</title> <
<!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> </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实现这一点?任何帮助都将不胜感激!
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
您有很多选择。最简单的,IMHO,应该是如下所示 将index.html文件重命名为index.php(确保web服务器查找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;
print所以问题是如何用PHP运行查询?为此,您应该使用PDO或mysqli。如果接受输入,查询应该参数化。@chris85我不知道这些是什么!有没有一种更简单的方法或者一个例子来从MySQL中读取一个列?你能把它缩减到最小的程度吗?这里有视频、FTP、HTML、PHP、Python、MySQL以及其他代码。我所希望的就是能够从MySQL中检索数据。这与要求提供教程非常接近,这是离题的。如果你不知道从哪里开始,我会看一看。它将为您指明正确的方向。当您可以只使用php标记时,为什么要使用herdoc呢(?>@t.m.adam我喜欢这种格式,因为它更易于阅读和维护。在代码中散布许多短标记是很难看的,也很难看到发生了什么。在这种情况下,只有两个地方需要使用短标记。这是个人偏好。如果你看我的答案,我会特别回答answer MySQL详细的编码答案。任何数量的PHP/MySQL编码教程都很容易回答这个问题。正如我所说的,我认为这个问题更多的是一个如何使用PHP输出HTML(或创建与他的index.HTML文件等效的文件)的问题.好的,我明白了。我使用标签,或者只是在字符串中嵌入变量,但正如你所说的,这是个人问题preference@mlewis54感谢您的回复,但我的问题是我不知道我的服务器(WebIOPi)是否允许我运行php!我知道如何引导它使用index.html并运行它。我正在使用raspberry pi 3和php插件Get MySQL。不需要多个index.php和其他文件,就可以直接将php代码放入index.html吗?我相信有一些乱七八糟的地方可以让您将php作为一个html文件运行,从而olve.htaccess文件规则。我对Webliopi作为web服务器的熟悉程度超出了我所读的内容。因为它看起来非常面向Python,所以我没有太多关注它。我建议使用NGINX甚至apache。我在Raspberry PI 3上安装了几个服务器,它们工作速度很快,可以支持大量连接。有一个很多伟大的功能,比如反向代理。设置起来相当容易。所有这些都说明你可以用PHP程序在谷歌上运行你的web服务器。
<?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;
?>
print <<< END
<<<what you want to print>>>
END;