0

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?

2
  • 1
    Where is session_start(); at your page?? Commented Jun 10, 2016 at 7:34
  • Notice : $_SESSION is undefined Commented Jun 10, 2016 at 7:40

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This part of php was included in another php that's why I didn't write it here, but I had the session_start() included. The problem was in my db, owner field had the values 1,etc and my user was user1, that's why it didn't print anything. Thank you anyway
0

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.

2 Comments

I wrote my solution in the first answer of the post. I gave you the correct answer because thanks to you I saw that it prints "user1" and then I realize that my owner field is different from the user and should be identically. Thanks
Yeah that's the reason i asked you to print the data .
0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.