简体   繁体   English

无法显示数据库php中的数据

[英]Cannot display data from the database php

I am working on a php project and I have a problem printing data from the database. 我正在处理一个php项目,从数据库打印数据时遇到问题。 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? 可能是什么问题?

You are missing session_start() 您缺少session_start()
That means your $user is not defined. 这意味着您的$user未定义。 That is the reason why there is no data fetched. 这就是为什么没有提取数据的原因。

Write session_start() at the top of your page to allow sessions to work on that page . 在页面顶部编写session_start() ,以允许会话在该页面上工作。 Then try printing the value of echo $user = $_SESSION['sess_user']; 然后尝试打印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. 或者,您可以打印sql语句以检查变量$user是否具有该值。

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

Hope this helps. 希望这可以帮助。

First check if the $user value is set correctly. 首先检查$ user值是否设置正确。 Then if it has any special characters you can use mysqli_real_escape_string . 然后,如果它有任何特殊字符,则可以使用mysqli_real_escape_string And in the table $row[id] should be $row['id']. 在表中$ row [id]应该是$ 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. 您可以简单地将表添加到html代码中,如下所示。

<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>

Hi You have not started session in this page. 您好您尚未在此页面中开始session We have to start session_start at every page where we want to use session values. 我们必须在要使用会话值的每个页面上启动session_start Session should be started at top of page before anything is displayed. 在显示任何内容之前,应在页面顶部开始Session 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 其次,请查看您的$user是否具有值,并添加mysqli_error()来检查查询是否存在任何error

$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>"; $display_string .= "<td>".$row['id']."</td>";

this should be done in all other lines 这应该在所有其他行中完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM