queries results empty

Problems with the Windows version of XAMPP, questions, comments, and anything related.

queries results empty

Postby dfghjk » 19. May 2021 07:25

Hi,

Since I use latest xampp release on windows 10 all sqli queries results are empty despite PhpMyAdmin shows all my databases are correct and full.
Previous version was with php 7 on windows 7 and everything was fine.

This is my php sqli code :

Code: Select all
      $host = "127.0.0.1";
      $user = "root";
      $psw = "";
      $db = "dbname1";


   function sqlQuery($query)
   {
      global $connection;
      $result = mysqli_query($connection, $query)  or die(mysqli_error($connection) . "<br/>" . $query . echopre(debug_backtrace()));
      return $result;
   };
   
   function sqlFetchObject($result)
   {
      return mysqli_fetch_object($result);
   };
   
   function sqlNumRows($result)
   {
      return $result->num_rows;
   };
   
   function sqlError()
   {
      return mysqli_error($connection);
   };
   
   function sqlFetchAssoc($result)
   {
      return mysqli_fetch_assoc($result);
   };
   
   function sqlFreeResult($result)
   {
      mysqli_free_result($result);
   }

   function Connect()
   {
      global $host, $user, $psw, $db;
      
      $connection = mysqli_connect($host, $user, $psw, $db);

      if (mysqli_connect_errno())
      {
         die("Failed to connect to MySQL : " . mysqli_connect_error() . "<br/>" . $query . echopre(debug_backtrace()));
      }

      $utf = "utf8";
      if(!mysqli_set_charset($connection, $utf))
         die("Can't set utf8");

      return $connection;
   }
   
   function sqlCloseConnection($connection)
   {
      mysqli_close($connection);
   }
?>


and a query :

Code: Select all
   public function LoadSubCats($catid)
   {
      $categories = array();
      $query = "SELECT * FROM categories
               WHERE categorie=" . (int)$catid . "
               ORDER BY position";
      $result = sqlQuery($query);
      while($obj = sqlFetchObject($result))
      {
         $categorie = new categorie();
         $categorie->fill($obj);
         $categories[(int)$obj->position] = $categorie;
      }
      
      return $categories;
   }


The result is an empty array.

Also print_r is broken so I can't even debug.
dfghjk
 
Posts: 1
Joined: 19. May 2021 07:16
XAMPP version: New XAMPP release 7.3.28, 7.4.
Operating System: windows 10

Re: queries results empty

Postby Nobbie » 19. May 2021 11:47

There are lots of programming errors, which definately results in unpredictable results. You are using $connection in many function without declaring it, for example:

Code: Select all
  function sqlError()
   {
      return mysqli_error($connection);
   };


That is unpredictable. Sometimes you declare $connection as global, sometimes not (then PHP creates a new local variable with the same name, but totally different scope). Seems that PHP7 did manage all these errors differently.

Same for the function Connect(), but we are also missing the code where you are using Connect(). I cannot see if you pass the return value of Connect() correctly to a global static variable. But you did not declare it as global inside Connect(). This mixture of passing parameters and/or using globals is really bad coding and it is definately the reason for your problems.
Nobbie
 
Posts: 13179
Joined: 09. March 2008 13:04

Re: queries results empty

Postby Altrea » 19. May 2021 11:55

Hi,

I have no idea how we should be able to help you solving this issue.
I have downloaded and installed a fresh version of XAMPP 7.3.28 and tested print_r() with this code
Code: Select all
<?php
class Dummy {
    public $id;
    public $name = '';
}



$obj = new Dummy();
$obj->id = 1;
$obj->name = 'Objectname';

print_r($obj);


It is ruinning fine.

I cannot test more because you did not provide a full test case i can reproduce on my test environment.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 197 guests