[英]Migrating from xamp to raspberry pi apache server
I'm currently working on a project. 我目前正在做一个项目。 It's almost done, there's only one big problem.
差不多完成了,只有一个大问题。 I tested my code all the time with a xamp server on my computer, which worked perfectly fine.
我一直在计算机上的xamp服务器上测试我的代码,效果很好。 the goal is to run it (apache server, mysql database) on my raspberry pi.
目标是在我的树莓派上运行它(apache服务器,mysql数据库)。 Now my project is finished, I came figured out the problem why my code doesn't work on my raspberry (at least not as I expected).
现在我的项目完成了,我想出了为什么我的代码在我的树莓上不起作用的问题(至少不是我期望的那样)。
I turned on error reporting in PHP and came to this error message: 我打开了PHP中的错误报告,并看到以下错误消息:
Notice: Trying to get property of non-object in /var/www/html/test.php on line 41
注意:尝试在第41行的/var/www/html/test.php中获取非对象的属性
I use this function for all my SQL queries. 我对所有SQL查询都使用此功能。 Can someone provide a solution so I don't have to rewrite the whole code?
有人可以提供解决方案,这样我就不必重写整个代码了吗? Thanks in advance!
提前致谢!
PS: this is just a piece of the code (the function where I pull the data out of the database + example of one of my queries) PS:这只是代码的一部分(我从数据库中提取数据的功能+一个查询的示例)
<?php
// Enable debugging
error_reporting(E_ALL);
ini_set('display_errors', true);
$servername = "localhost";
$username = "root";
$password = "*****"; // I just dont want to give my sql database password its nothing wrong ;)
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
print_r("ok connection");
function sqlquery ($sql, $conn, $naamtabel) {
global $myArray;
global $stateLoop;
$stateLoop = "0";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //line 41 in my code ==> do a while loop to fetch all data to an array
// output data of each row
while($row = $result->fetch_assoc()) {
$myArray[] = $row["$naamtabel"]; //alle data van kolom "tijd" in een array
}
$stateLoop = "1";
}
else { // if there are no results
}
}
$sql1 = "SELECT stopTijd FROM gespeeldeTijd WHERE naam = 'thomas' ORDER BY ID DESC LIMIT 1"; // get data with SQL query
sqlquery($sql1,$conn,"stopTijd");
if ( $stateLoop == "1") {
print_r("ok loop");
$date1 = $myArray["0"];
print_r($date1);
$myArray = [];
$stateLoop == "0";
}
}
?>
It pretty much looks like you have some sql error in your query; 看起来您的查询中似乎有一些sql错误; check if your field names in your database match those on the raspberry.
检查数据库中的字段名称是否与树莓派上的字段名称匹配。
Seeing through your code it seems like you are pretty new to programming (which is no bad thing, I was once, too). 通过查看代码,您似乎对编程还很陌生(这也不是一件坏事,我也是一次)。 So I made a few more modifications to your code showing you the prettiness of PHP
因此,我对您的代码做了一些修改,向您展示了PHP的美感。
I commented everything I changed 我评论了我所做的一切
<?php
// Enable debugging
error_reporting(E_ALL);
ini_set('display_errors', true);
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "test";
// Your function with some modifications
function sqlquery($sql, $conn, $naamtabel) {
$result = $conn->query($sql);
// Check for errors after execution
if(!$result)
die('mysqli error: '. htmlentities(mysqli_error($con)));
// If we have no data, we simply return an empty array
if($result->num_rows == 0)
return array();
// This is a variable we store the data we processed in
// We will return it at the end of our function
$myArray = null;
// Read all field data and store it $myArray
while($row = $result->fetch_assoc())
$myArray[] = $row[$naamtabel]; // if you use "$naamtabel" here, PHP first needs to interpret the string (= slower)
return $myArray;
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
// Because we use "die" above we don't need an "else"-clause
print_r("ok connection");
$sql = "SELECT `stopTijd` FROM `gespeeldeTijd` WHERE `naam` = 'thomas' ORDER BY `ID` DESC LIMIT 1";
$data = sqlquery($sql, $conn, "stopTijd");
// $data will contain $myArray (see "return $myArray" in function sqlquery)
// Instead checking for $stateLoop being "1" we check if $data contains any values
// If so, we fetched some data
if(sizeof($data) >= 1) {
print_r("ok loop");
$date1 = $data[0]; // No "0", because we are trying to get index 0
print_r($date1);
$data = array(); // Are you sure this is nessecary?
} else {
echo 'No data returned from query!';
}
?>
Note: code tipped on my smartphone -> untested! 注意:我的智能手机上提示输入的代码->未经测试!
If you don't want to adapt the code I wrote, the important part for this question is: 如果您不想改写我编写的代码,那么此问题的重要部分是:
if(!$result)
die('mysqli error: '. htmlentities(mysqli_error($con)));
Your error Notice: Trying to get property of non-object
means "you are trying to get num_rows
from $result
, but $result
is not an object, so it can't contain this property". 错误
Notice: Trying to get property of non-object
意味着“您正在尝试从$result
获取num_rows
,但是$result
不是对象,因此它不能包含此属性”。
So to figure out why $result
is not an object, you need to get the error from $conn->query
- my code above probably won't fix your error, but it will display you one you can work with (+ it's too long for a comment) 因此,要弄清楚为什么
$result
不是对象,您需要从$conn->query
获取错误-上面的代码可能无法修复您的错误,但是会显示一个您可以使用的错误(+也是渴望发表评论)
If you have a more detailed error message and you can't solve it on your own, feel free to comment; 如果您有更详细的错误消息,并且无法自行解决,请随时发表评论; I will update my answer!
我将更新我的答案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.