php - 找不到行的值

标签 php mysql row

这是我的 PHP 代码:

$json = file_get_contents('php://input');
$userInfo = json_decode($json);
$id = $userInfo->id_user;

include "DatabaseManager.php";
$db = new DatabaseManager();
$username = $db->getInfoUser($id);

  function getInfoUser($id)
  {
      $connection = mysqli_connect("localhost", "root", "", DatabaseManager::DATABASE_NAME);
      $sqlCommand = "SELECT username FROM users WHERE id = '$id'";
      $result =$connection->query($sqlCommand);
      if($result->num_rows >0){
          while ($row = $result->fetch_assoc()){
              echo json_encode(['username' => $row['username'],"url_profile_img"=> $row['img_profile']]);
          }
      }
  }

此代码返回 $row['username'] 的值没有问题,但是 $row['img_profile'] 返回 null! 数据库中的 img_profile 存在但返回 null。

我该怎么办?

最佳答案

您没有在 SQL 中选择 img_profile...

SELECT username FROM users WHERE id = '$id'

也许

SELECT username, img_profile FROM users WHERE id = '$id'

同样值得研究如何使用准备好的语句来帮助保护您的代码。

关于php - 找不到行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907547/

相关文章:

javascript - 返回到 ajaxrequest 的 PHP 对象有 boolean == true 而不是 data

php - 更新数据库当前字段

php - 我想连同 304 响应一起发送哪些 header ?

mysql - 使用 SQL 从同一个表中的两行中选择

r - 在 R : remove rows containing no integer (such as characters i. e.) 中,来自数据帧

php - 如何在php中从数据库中的文本框中写入文本

mysql - 将数据并发插入 MySQL 的更快方法

Php代码不向数据库发送查询

类似安卓中继器的功能

ios - SQLite 数据库中的行顺序 (iOS)