Home php Help Notice: Trying to get property of non-object

Help Notice: Trying to get property of non-object

Author

Date

Category

It is necessary to display a list of all users in the system, I don’t understand a bit how to do this with the RedBeanPHP framework. I’ll be very thankful. The error occurs in the foreach loop. database structure – table user with fields id , login , password2

$ user = R :: findAll ('user');
  if ($ user) {
    foreach ($ user as $ login = & gt; $ id) {
      # code ...
      echo 'id'. $ user- & gt; id. ' login '. $ user- & gt; login.' & lt; br & gt; ';
    }
  }

Answer 1, authority 100%

The

error tells you literally “trying to access a property on a non-object” . In the code, you select a list of users and, obviously, you get an array of objects (or arrays). Next, you try to access $ users- & gt; id , which causes an error, since already said. that it is an array of objects.

Didn’t read what RedBean returns there, but in the case of objects:

foreach ($ user as $ u) {
  echo 'id'. $ u- & gt; id. ' login '. $ u- & gt; login.' & lt; br & gt; ';
 }

in case of arrays

foreach ($ user as $ u) {
  echo 'id'. $ u ['id']. ' login '. $ u [' login '].' & lt; br & gt; ';
 }

Answer 2

Get properties not from the $ user array, but from the next selected element of this array

$ user = R :: findAll ('user');
  if ($ user) {
    foreach ($ user as $ u) {
      # code ...
      echo 'id'. $ u- & gt; id. ' login '. $ u- & gt; login.' & lt; br & gt; ';
    }
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions