我有一个最奇怪的问题。我已经有了apache2服务器和php5超过2年在我的Raspberry Pi上运行一些东西。 php中的一个简单代码,用于连接mySQL并获取/显示一些数据。最近我的sdhc卡与raspbian os一起死了,因此我不得不重新设置一切。我的代码在github上,谢天谢地,我只是从那里下载了它。
由于某些原因,我的php不会连接并且什么都不显示,基本上页面无法显示。我确实检查了其他PHP代码,他们工作。我不明白的是,因为我设置了具有完全相同参数的sql数据库,为什么php不能正常工作?
要检查,我运行我的本地python代码,负责连接到同一个mysql数据库并填充它,旧代码工作得很好。我真的不知道php没有显示任何消息并且无法正常工作有什么问题。我忽略了什么吗?或者只是错过了我必须设置的东西?
服务器的php代码,保存为mysql.php:
<?php
//Step 1
$conn = mysqli_connect('localhost', 'zikmir', 'gforce', 'temp_database') or die('Error connecting to MySQL server.');
$dbhost = "localhost";
$dbuser = "zikmir";
$dbpass = "gforce";
$dbname = "temp_database";
$page = $_SERVER['PHP_SELF'];
$sec = "5";
header("Refresh: $sec; url=$page");
?>
<html>
<head>
</head>
<body>
<h1>
PHP connect to MySQL
</h1>
<?php
//Step 2
$sql = "SELECT Id, time, temp FROM time_temp ORDER BY Id DESC LIMIT 0,1";
$result = $conn->query($sql) or die('Error querying database.');
while ($row = $result->fetch_assoc()){
echo "ID " . $row["Id"]. " Time:" . $row["time"]. " Temp: " . $row["temp"]. "<br>";
$conn->close(); }
?>
</body>
</html>
我的新数据库设置与之前相同,至少我认为。数据库信息如下:
database name: temp_database
user: zikmir
pass: gforce
table name: time_temp
column: Id, time, temp
SELECT *FROM time_temp;
+----+-----------------------------+------------+
| Id | time | temp |
+----+-----------------------------+------------+
| 1 | 0000-00-00 00:00:00 | 25 |
| 2 | 0000-00-00 00:00:00 | 26 |
| 3 | 2017-10-20 04:03:42 | 22.687 |
| 4 | 2017-10-20 04:04:14 | 22.75 |
| 5 | 2017-10-20 04:04:47 | 22.687 |
| 6 | 2017-10-20 04:05:28 | 22.687 |
| 7 | 2017-10-20 04:09:40 | 26 |
| 8 | 2017-10-20 04:11:26 | 26 |
+----+-----------------------------+------------+
这些值是手动填充的,前几个是由python程序完成的,它仍然可以正常运行。这是崩溃前使用的相同程序。因此,没有代码被更改,像以前一样工作并填充数据库。 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('0',now(),%s)"
cursor.execute (sql,(temp,))
db.commit()
# Wait 5 seconds
time.sleep(30)
如上所述,没有更改代码,只是从github下载。仅使用相同的表名e.t.c重新创建数据库。任何帮助将非常感谢!
答案 0 :(得分:0)
它有效,特别感谢Magnus我能够发现mysqli库已经丢失了。通过以下命令进行简单安装:
sudo apt-get install php5-mysqlnd
并添加这些文件,如果它们不存在于php.ini文件中:
extension=msqli.so
extension=msql.so
在此之后,它就像一个魅力!