0

I created a website on Windows using PHP, MySQL and one Line of JS, and tested it with XAMPP. Works perfectly. If I load this page on my PI now, it doesn't work anymore. The php script stop working. PHP is up-to-date. I can't update MariaDB because the latest available version for the PI is 10.3.23-MariaDB-0+deb10u1.

Both times I get the error message :

PHP Notice:  Undefined index: SERVER_NAME in /var/www/test/inc/functions.php on line 5
PHP Fatal error:  Uncaught mysqli_sql_exception: No index used in query/prepared statement (null) in /var/www/test/inc/functions.php:497
Stack trace:
#0 /var/www/test/inc/functions.php(497): mysqli_stmt_execute()
#1 /var/www/test/opener.php(8): checkCurrentTimer()
#2 {main}
  thrown in /var/www/test/inc/functions.php on line 497

using mysqli_report(MYSQLI_REPORT_OFF); this error is no longer displayed.

What is my error? The databases on the PI and my PC are identical (exported from the PC, imported into the PI and checked).

            <?php
//Looks like the next line causes the problem
            if (checkCurrentTimer(0)){
               echo '<div class="col">
                    <form method="post" action="'.$_SERVER['PHP_SELF'].'" style="margin-left:auto;margin-right:auto;display:flex;">
                        <input type="number" name="number" placeholder="Sec" style="max-width:60px"><p> </p>
                        <button type="submit" class="btn btn-block btn-dark" value="Zeitschaltung" name="Zeitschaltung" style="font-size:18px;">Zeitschaltung</button>
                    </form>
                </div>  ';
            }else{
                if(strtolower(checkCurrentTimer(1)) == strtolower($_SESSION['session_user'])){
                   ?>
                    <script src="/inc/js/jquery-3.5.1.js"></script>
                    <script type="text/javascript">
                    <!-- setTimeout("document.location.reload();",1000); -->
                   var initialTime = <?php echo timeleft($_SESSION['session_user']);?>;
                    var seconds = initialTime - 1;
                    function timer() {
                        var remainingSeconds = seconds;
                        if (remainingSeconds < 10) {
                            remainingSeconds = "0" + remainingSeconds;
                        }
                        document.getElementById('timer').innerHTML = "Dein Timer läuft noch <br>" +remainingSeconds+ "<br>Sekunden";
                        if (seconds == 0) {
                            clearInterval(countdownTimer);
                            document.getElementById('timer').innerHTML = "Completed";
                        } else {
                            seconds--;
                        }
                    }
                    var countdownTimer = setInterval('timer()', 1000);
                  </script>

                            <div class="col">
                                <form id="timer" class="btn btn-block btn-dark" name="return" style="color:green;font-size:14px;width:200px;margin-left:auto;margin-right:auto">
                                    Dein Timer läuft noch <br> <?php echo timeleft($_SESSION['session_user']);?><br> Sekunden
                                </form>
                            </div>

                   <?php
                }else{
                    echo '
                        <div class="col">
                            <form class="btn btn-block btn-dark" name="return" style="color:orange;font-size:14px;width:200px;margin-left:auto;margin-right:auto"">
                                Ein Timer läuft aktuell
                            </form>
                        </div>
                       ';
                }
            }
            ?>

The function:

function checkCurrentTimer($arg){
    include 'db.php';
    switch($arg){
        case 0:
            $sql = "SELECT COUNT(*) AS counts FROM timer;";
            $stmt = mysqli_stmt_init($connection);
            if (!mysqli_stmt_prepare($stmt, $sql)){
                // Leitet auf die Einstellungsseite weiter und meldet das ein Fehler aufgetreten ist
                mysqli_stmt_close($stmt);
                mysqli_close($connection);
                header("Location: ../module/oneklick.php?error=sqlerror&m=54");
                exit();
            }else{
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                $row = mysqli_fetch_assoc($result);
                $counts = (int)$row['counts'];
                if($counts == 0 ){
                    mysqli_stmt_close($stmt);
                    mysqli_close($connection);
                    return true;
                }elseif($counts > 0 ){
                    mysqli_stmt_close($stmt);
                    mysqli_close($connection);
                    return false;
                }

            }
            break;
        case 1:
            $sql = "SELECT user FROM timer";
            $stmt = mysqli_stmt_init($connection);
            if (!mysqli_stmt_prepare($stmt, $sql)){
                // Leitet auf die Einstellungsseite weiter und meldet das ein Fehler aufgetreten ist
                mysqli_stmt_close($stmt);
                mysqli_close($connection);
                header("Location: ../opener.php?error=sqlerror&m=55");
                exit();
            }else{
                mysqli_stmt_execute($stmt);
                $result = mysqli_stmt_get_result($stmt);
                $row = mysqli_fetch_assoc($result);
                $user = $row['user'];
                if(empty($row['user'])){
                    $user = "%Empty%" ;
                }
                mysqli_stmt_close($stmt);
                mysqli_close($connection);
                return $user;
            }
            break;
        default:
            header("Location: ../opener.php?error=sqlerror&m=55");
            exit();
    }
}

Pi phpmyadmin: https://prnt.sc/uq5rgw

XAMPP phpmyadmin: https://prnt.sc/uq5rur

DB table timer structure: https://prnt.sc/uq5scx

Dharman
  • 30,962
  • 25
  • 85
  • 135
Indêx
  • 9
  • 2
  • 1
    Does this answer your question? [Fatal error: Uncaught exception 'mysqli\_sql\_exception' with message 'No index used in query/prepared statement'](https://stackoverflow.com/questions/5580039/fatal-error-uncaught-exception-mysqli-sql-exception-with-message-no-index-us) (be sure to look at most upvoted, not just the accepted answer). – ficuscr Sep 29 '20 at 20:15
  • I recommend comparing your `php.ini` files between the XAMMP and PI variants. Calling `phpinfo();` will report the environment values. It may be broken in the XAMMP version as well but due to the ini settings, the exception is not thrown. Instead of using conditional `if (mysqli_stmt_prepare())` I suggest you utilize [`try/catch` exception handling](https://www.php.net/manual/en/mysqli-driver.report-mode.php) `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. – Will B. Sep 29 '20 at 20:31
  • @WillB. The site works on the PC. the sql query gets executed perfectly – Indêx Sep 30 '20 at 17:55
  • It being "executed perfectly", does not mean there isn't a warning that is being ignored. My advice is to compare the config settings for PHP and MariaDB between the two environments to determine what is different, that will point you to what is causing the issues in your PI environment. Every distro of PHP and MariaDB that you download and install from the web has differing default settings, depending on the repository and version installed. – Will B. Sep 30 '20 at 19:25
  • @WillB. ok Thanks i will check it. – Indêx Sep 30 '20 at 19:41

0 Answers0