-3

I do a query but does not show anything on the screen and when I opened the page where I do the query is slow and does not show anything

$query="SELECT * FROM Dettagli_macchina WHERE  macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
                    while($row=mysqli_num_rows($result)){
                        echo $row['COMPONENTE'];

                    }
Drew
  • 24,851
  • 10
  • 43
  • 78
mario lucci
  • 19
  • 1
  • 5
  • While with mysqli_num_rows???? U will use mysqli_fetch_array here. mysqli_num_rows will return no of rows not rowData – devpro Oct 04 '16 at 17:02
  • 4
    Its slow because loop is infinite always true – devpro Oct 04 '16 at 17:04
  • Now u have lot of solutions try it. – devpro Oct 04 '16 at 17:25
  • When you say DOES NOT WORK.. please please please explain what you mean. The code should work perfectly, so either you have no matching records or something is wrong with your SQL query, which we can't tell because we know nothing about your database. – Duane Lortie Oct 04 '16 at 17:51

4 Answers4

4

mysqli_num_rows() will only use for getting no of rows not for row data.

You need yo use mysqli_fetch_*()

while($row=mysqli_fetch_array($result)){ 
    echo $row['COMPONENTE']; 
}

Why this query slow? Because you are using infinite loop here, always TRUE.

while($row=mysqli_num_rows($result))

One more story, I hope you are using session_start() in your file, but suppose that if $_SESSION not found or not start than your query will failed.

In last, this is just a suggestion regarding Naming Convention, you are using column name in small letter, capital small, full capital, this is not related to answer but you must need to learn about this art.


this will help you to understand Naming Convention: Database, Table and Column Naming Conventions?

This reference will help you to understand how mysqli_fetch_array() works: http://php.net/manual/en/mysqli-result.fetch-array.php

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38
0

You are fetching record but using mysqli_num_rows() which return total number of rows/records replace it with mysqli_fetch_array() here is the working example.

$query="SELECT * FROM Dettagli_macchina WHERE  macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row = mysqli_fetch_array($result)) { //replace mysqli_num_rows with mysqli_fetch_array 
      echo $row['COMPONENTE'];
    }
Kamran Khatti
  • 3,754
  • 1
  • 20
  • 31
0

Please try with this one for return value and if you need number of raw then the statements will be different.

  $query="SELECT * FROM Dettagli_macchina WHERE  macchine_id='$macchine' and Email='$_SESSION[login_user]'";
    $result = mysqli_query($conne,$query);
                        while($row=mysqli_fetch_array($result)){
                            echo $row['COMPONENTE'];

                        }
Liakat Hossain
  • 1,288
  • 1
  • 13
  • 24
0

As others have pointed out, you have mixed up the mysqli_num_rows function, here I am using it to print the number of results found, then loop through the results after converting the mysqli result object to an array named $row

    echo 'Found '. mysqli_num_rows($result) .' results';

    while ($row = mysqli_fetch_array($result)) { 
    echo $row['COMPONENTE'];
   }
Duane Lortie
  • 1,285
  • 1
  • 12
  • 16