View report from Mysql tables

Asked

Viewed 596 times

1

I have a problem generating a report, in which the site administrator will see the fields ID and login 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 executing, which returns an error:

Notice: Trying to get Property of non-object "localfile" online 9

And honestly, I didn’t understand the reason for this mistake or how to fix it.

  • 2

    recommended reading: http://answall.com/a/4675/13561

  • what is localarquivo and which is line 9?

  • "Localfile" is the directory in which my system is located, I chose not to show it. @Jorgeb.

  • Line 9 is line while($row = $exibe->fetch_assoc()) {

  • 2

    Turns out you’re mixing mysql_ and mysqli_. Only with the new version of mysqli_ is that you can use as an object $exibe->fetch_assoc(). With mysql_ you can only use mysql_fetch_assoc($exibe). I advise you to change everything to mysqli_, for more information read the link of Sanction.

  • If you change from mysql (in depreciation) to mysqli, consider taking a look at PDO (http://php.net/manual/en/class.pdo.php)

Show 1 more comment

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, the 0 is passed as string before a quantitative comparison operator. The example will work, but it is always better to treat an integer as an integer.

Of:

$exibe-> num_rows > '0'

To:

mysql_num_rows($exibe) > 0

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

Of:

$row = $exibe->fetch_assoc()

To:

$row = mysql_fetch_assoc($exibe)

The functions generate errors, for methods called from variables that are not objects. In this case, returned a E_NOTICE

Notice: Trying to get Property of non-object "localfile" online 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 the functions Mysql are obsolete, thus giving rise to the new and safer functions of the Mysqli which is a version of the old Mysql, but safer, or you can also use the PDO.

You can also see in this example, how your script would look, with the 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 the PDO here in the Sopt, just search them using the search bar on website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.