2017-10-20 132 views
0

我有史以来最奇怪的问题。我已经有apache2服务器和php5一起运行了超过2年,在我的Raspberry Pi上运行了一些东西。一个简单的代码在PHP中连接到MySQL并获取/显示一些数据。最近我的SDHC卡与raspbian os一起死亡,因此我不得不重新设置所有东西。我的代码在github上,所以谢天谢地我只是从那里下载了它。RasPi上的php5和mysql之间的连接错误

出于某种原因,我的PHP不会连接和显示什么,基本上页面无法显示。我没有检查其他的PHP代码,他们的工作。我不明白的是,因为我用完全相同的参数设置了sql数据库,为什么不是php不工作?

为了检查,我运行了我的本地python代码,负责连接到相同的mysql数据库并填充它,并且旧代码工作得很好。我真的不知道什么是错误的PHP没有显示任何消息,不工作。我可以俯视一下吗?或者只是错过了我必须安装的东西?

服务器,保存为mysql.php 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重新创建。任何帮助将非常感谢!

+1

你检查了你的错误日志吗?一个好主意也是在本地PHP环境中开启'display_errors'。在这里阅读更多:[我如何得到PHP错误显示?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) –

+0

或者你有没有转动PHP的错误报告以获取有意义的错误消息? – Shadow

+0

你们俩都是天才! @MagnusEriksson我看到的错误是这样的: 致命错误:调用未定义的函数mysqli_connect()在/var/www/html/mysql.php在第6行。我的下一个问题是为什么不是mysqli_connect()不是加工?是由于缺少一个php灌输库吗? –

回答

0

它的工作,特别感谢马格努斯我能够发现mysqli库失踪。简单的通过这个命令来安装它:

sudo apt-get install php5-mysqlnd 

,并添加这些文件,如果在php.ini文件中它们不存在:

extension=msqli.so 
extension=msql.so 

这一点,工作就像一个魅力了!