You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

欢迎页取数据库数据遇错误:尝试访问null类型值的数组偏移量

Fixing "Trying to access array offset on value of type null" in Your Session Code

Hey there, let's figure out why you're hitting that error! The notice means your $row variable is null when you try to access $row['nume'] on line 10—this happens when your database query doesn't return any matching user data. Here's how to diagnose and fix this step by step:

1. Make sure your session is set up correctly

First, move session_start() to the very top of your file (before including any other files) to avoid issues with headers already being sent. Also, always validate that $_SESSION['login_user'] exists and has a value before using it in a query:

<?php
session_start(); // Move this to the top

// Check if the session variable exists and isn't empty
if(!isset($_SESSION['login_user']) || empty($_SESSION['login_user'])){
    header("location:login.php");
    die();
}

include('config.php');
$user_check = $_SESSION['login_user'];

2. Fix your database query (and avoid SQL injection!)

Your current query directly inserts the session value into SQL, which is a huge security risk (SQL injection) and can break the query if the username has special characters (like a single quote). Use prepared statements instead, and add error handling to catch query issues:

// Replace your old query with this prepared statement
$stmt = mysqli_prepare($db, "SELECT nume FROM users WHERE nume = ?");
mysqli_stmt_bind_param($stmt, "s", $user_check);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

// Check if the query ran successfully
if(!$result){
    die("Query failed: " . mysqli_error($db));
}

3. Handle cases where no user is found

Even if the query runs, there might be no user matching the session value. Always check if $row is not null before accessing its values:

$row = mysqli_fetch_assoc($result);
if(!$row){
    // No user found—redirect to login
    header("location:login.php");
    die();
}

$login_session = $row['nume'];

Full corrected code

Putting it all together, your session.php should look like this:

<?php
session_start();

if(!isset($_SESSION['login_user']) || empty($_SESSION['login_user'])){
    header("location:login.php");
    die();
}

include('config.php');
$user_check = $_SESSION['login_user'];

$stmt = mysqli_prepare($db, "SELECT nume FROM users WHERE nume = ?");
mysqli_stmt_bind_param($stmt, "s", $user_check);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if(!$result){
    die("Query failed: " . mysqli_error($db));
}

$row = mysqli_fetch_assoc($result);
if(!$row){
    header("location:login.php");
    die();
}

$login_session = $row['nume'];
?>

This fixes the null error, makes your code more secure, and adds safeguards to handle edge cases where the session or user data is missing.

内容的提问来源于stack exchange,提问作者Andrei Mirescu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:08:10