私は今まで以上に奇妙な問題を抱えています。私は私のラズベリーパイにいくつかのものを実行して2年以上php5と一緒にapache2サーバーを持っています。 mySQLに接続し、いくつかのデータを取得/表示するPHPの簡単なコード。最近私のsdhcカードはraspbianのosと一緒に死んだので、私はすべてを再セットアップしなければならなかった。私のコードはgithub上にあったので、ありがたいことに私はそこからすべてをダウンロードしました。RasPi上のphp5とmysql間の接続エラー
私のPHPは何も接続せずに何も表示しません。基本的にページは表示できません。私は他のPHPコードをチェックし、彼らは動作します。私が理解していないのは、私がセットアップしたSQLデータベースとまったく同じパラメータで、なぜPHPが動かないのですか?
チェックするには、ローカルのpythonコードを実行して、同じmysqlデータベースに接続してデータを取り込み、古いコードを正常に動作させる必要があります。私は本当に何が間違っているのかわかりません。何か見落としていますか?それとも、私がセットアップしなければならなかった何かを逃したのですか?
の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 |
+----+-----------------------------+------------+
これらの値は、第1のウェルまだ完璧に働いているの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を持つデータベースのみが再作成されました。どんな助けも非常に高く評価されるでしょう!
エラーログを確認しましたか?あなたのローカルPHP環境で 'display_errors'を有効にすることをお勧めします。もっと詳しく読む:[PHPエラーを表示する方法は?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) –
また、意味のあるエラーメッセージを得るためのエラー報告? – Shadow
あなたは両方とも天才です! @MagnusEriksson私が見ているエラーはこれです: 致命的なエラー:行6の/var/www/html/mysql.phpの未定義関数mysqli_connect()を呼び出してください。次の質問は、なぜmysqli_connect()がワーキング?それはphp滴下のライブラリがないためですか? –