Display report of MySQL tables

1

I have a problem generating a report, in which the site administrator will see the ID and login fields of all registered users, so far, I have this code:

<?php
require_once('config.php');
$emite = "SELECT ID, login FROM usuarios";
$exibe = mysql_query($emite);
if ($exibe-> num_rows > '0') {
     while($row = $exibe->fetch_assoc()) {
         echo "<br> id: ". $row["ID"]. " - login: ". $row["login"]. "<br>";
     }
} else {
     echo "0 resultados";
}
?>
So far so good, the problem occurs at the time of execution, which returns an error:

  

Notice: Trying to get property of non-object "localarticle" on line 9

And honestly, I did not understand the reason for this error or how to fix it.

    
asked by anonymous 16.10.2015 / 13:01

1 answer

1

MySQL does not support object-oriented style, and in your script there are at least two references to class methods.

In this paragraph, in addition to the reference in the oriented style, 0 is passed as string in front of a quantitative comparison operator. The example will work, but it is always better to treat an integer as an integer.

From:

$exibe-> num_rows > '0'

To:

mysql_num_rows($exibe) > 0

Here is the same thing, reference to methods of a class that does not even exist.

From:

$row = $exibe->fetch_assoc()

To:

$row = mysql_fetch_assoc($exibe)

Functions generate errors for methods called from variables that are not objects. In this case, you returned an E_NOTICE

  

Notice: Trying to get property of non-object "localarticle" on line 9

Let's also assume that in your config.php file are the connection variables, and also where you select the database, since you are using MySQL .

...

$emite = "SELECT ID, login FROM usuarios";

$exibe = mysql_query($emite);
if (mysql_num_rows($exibe) > 0) {
     while($row = mysql_fetch_assoc($exibe)) {
         echo "<br> id: ". $row["ID"]. " - login: ". $row["login"]. "<br>";
     }
} else {
     echo "0 resultados";
}

Another thing is that MySQL functions are obsolete, thus giving rise to the new and more secure functions of MySQLi which is a version of the old MySQL , but more secure, or you can also use PDO .

You can also see in this example how your script would look with MySQLi :

$conexao = mysqli_connect("localhost", "usuario", "senha", "banco_de_dados");

$emite = "SELECT ID, login FROM usuarios";

$exibe = mysqli_query($conexao, $emite);
if (mysqli_num_rows($exibe) > 0) {
     while($row = mysqli_fetch_assoc($exibe)) {
         echo "<br> id: ". $row["id"]. " - login: ". $row["login"]. "<br>";
     }
} else {
     echo "0 resultados";
}

Some references:

There are also many good answers about MySQLi and PDO here in SOpt , just search them using < a href="http: //en.stackoverflow.com#search"> site .

    
17.10.2015 / 04:10