简体   繁体   中英

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. the goal is to run it (apache server, mysql database) on my raspberry pi. 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:

Notice: Trying to get property of non-object in /var/www/html/test.php on line 41

I use this function for all my SQL queries. 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)

<?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; 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

  • use "return" in function sqlquery instead of globals
  • check for errors after executing the code
  • use only one variable to check if data was loaded

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".
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)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM