Irina Farcau
Irina Farcau

Reputation: 167

Cannot display data from the database php

I am working on a php project and I have a problem printing data from the database. The connection is working, the empty table is printed but when it comes to print the data, nothing is showed.My currently code:

 <?php


  $con = mysqli_connect('localhost','root','root', 'db'); 

  $user = $_SESSION['sess_user'];


  $query=("SELECT * FROM urls WHERE owner = '".$user."'");

  $qry_result = mysqli_query($con,$query);

  $display_string = "<table>";
  $display_string .= "<tr>";
  $display_string .= "<th> Id</th>";
  $display_string .= "<th> Url</th>";
  $display_string .= "<th> Owner </th>";
  $display_string .= "<th> SharingUrls </th>";
  $display_string .= "</tr>";

 while($row = mysqli_fetch_array($qry_result)) {//doesn't enter in this loop
    echo 'sdf';  //?? nothing
    $display_string .= "<tr>";
      $display_string .= "<td>$row[id]</td>";
      $display_string .= "<td>$row[url]</td>";
      $display_string .= "<td>$row[owner]</td>";
      $display_string .= "<td>$row[sharingUsers]</td>";

      $display_string .= "</tr>";
  }

  $display_string .= "</table>";

  echo $display_string;


?>

What can be the problem?

Upvotes: 0

Views: 199

Answers (4)

Passionate Coder
Passionate Coder

Reputation: 7294

Hi You have not started session in this page. We have to start session_start at every page where we want to use session values. Session should be started at top of page before anything is displayed. Second please see your $user if it has value or not and add mysqli_error() to check if query has any error.so add this

$qry_result = mysqli_query($con,$query) or print_r(mysqli_error($con));

And please change this line

$display_string .= "<td>$row[id]</td>";

with $display_string .= "<td>".$row['id']."</td>";

this should be done in all other lines

Upvotes: 0

sanurah
sanurah

Reputation: 1122

First check if the $user value is set correctly. Then if it has any special characters you can use mysqli_real_escape_string. And in the table $row[id] should be $row['id']. I don't understand why you are creating a string. You can simply add the table in your html code with something like this.

<table>
    <thead>
    <tr>
        <th>id</th>
        <th>url</th>
        <th>owner</th>
        <th>sharingusers</th>
    </tr>
    </thead>
    <tbody>
    <?php
    if(isset($qry_result ) && !empty($qry_result )){
        while($row = $qry_result->fetch_assoc()){
            echo "
        <tr>
        <td>{$row['id']}</td>
        <td>{$row['url']}</td>
        <td>{$row['owner']}</td>
        <td>{$row['sharingusers']}</td>
        </tr>\n";
        }
    }
    ?>
    </tbody>
</table>

Upvotes: 0

Megan Fox
Megan Fox

Reputation: 435

Write session_start() at the top of your page to allow sessions to work on that page . Then try printing the value of echo $user = $_SESSION['sess_user'];

Once the value is got it should get the data from database . Alternately you can print the sql statement to check if the variable $user has the value.

echo $query=("SELECT * FROM urls WHERE owner = '".$user."'");

Hope this helps.

Upvotes: 0

Praneeth
Praneeth

Reputation: 83

You are missing session_start()
That means your $user is not defined. That is the reason why there is no data fetched.

Upvotes: 2

Related Questions