I am having the weirdest issue ever. I have had apache2 server along with php5 for more than 2 years running some stuff on my Raspberry Pi. A simple code in php to connect to mySQL and fetch/display some data. Lately my sdhc card died along with the raspbian os thus i had to re-setup everything. My code was on github so thankfully i just downloaded it all from there.
For some reason my php wont connect and shows nothing, basically page cannot be displayed. I did check other php codes and they work. What i don't understand is since i setup the sql data base with exact same parameters why isn't the php not working?
To check, i ran my local python code that is responsible to connect to the same mysql database and populate it, and the old code worked just fine. I really don't know what is wrong with php not showing any message and not working. Am i overlooking something? Or simply missing out something that i had to setup?
The php code for the server, saved as 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>
My new data base is set identically to what i had before, at least i think. The database info is as follows:
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 |
+----+-----------------------------+------------+
These values where populated manually, well the first few and rest by a python program which is still working perfectly. It was the same program used before the crash. So, no code is changed, works like before and populates the database. The python code is:
# 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)
As mentioned, no code was changed, just downloaded from github. Only the database was re-created with same table names, e.t.c. Any help would be very much appreciated!