4

problem resolved (in comments) i am making a project for school and i am getting this error Trying to access array offset on value of type null on this line of code

<div class="user-name"><?php echo $guests["firstName"] ?></div>

it is in this code and i retrieve the information out of a database

foreach((array)$result as $guests)
        {
    ?>
    <div class="posts-container">
        <div class="post-header">
            <div class="user-details">
                <div class="user-name"><?php echo $guests["firstName"] ?></div>
                <div class="user-email"></div>
            </div>
            <div class="time"></div>
        </div>
        <div class="post-message">
            <h3></h3>
            <p></p>
        </div>
    </div>
    <?php } ?>

people asked for the sql code and the type/structure of $result type/structure: object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(9) ["lengths"]=> NULL ["num_rows"]=> int(5) ["type"]=> int(0) sql/database:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "guestbook";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstName, lastName, email, title, message, URL, showEmail, Date FROM guestbook";
$result = $conn->query($sql);

$conn->close();
6
  • 1
    if your array is sub array then use foreach($result as $k => $guests) Commented Jan 21, 2021 at 9:17
  • 1
    Please also post the array.But @HammadAhmedkhan may be right. Commented Jan 21, 2021 at 9:19
  • Did you debug your code - often a few var_dump()s will help you see what's going on? Start with a var_dump($result) to see what data (type/structure) you actually have - share that with us. Commented Jan 21, 2021 at 9:39
  • Your $guest value is null. See 3v4l.org/oOXmO Commented Jan 21, 2021 at 10:57
  • Does this answer your question? "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP Commented Jan 26, 2021 at 8:45

3 Answers 3

1

You are trying to access to a null value as an array.

In your case, $guest value is null. So, accessing to $guest['something'] will throw a "Notice: Trying to access array offset on value of type null".

Two suggestions :

  1. Check $guest value (quick) :

    foreach((array)$result as $guests)
    {
        if (!is_array($guests)) {
            continue;
        }
    
  2. Check $result value (better) :

    If $result is a result of a SQL query, maybe try to add conditions to avoid NULL results. Or, use array_filter() before to remove empty values.

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

1 Comment

the first option worked but i the foreach doesn't give results and the second option i get a error array_filter() expects parameter 1 to be array, object given in
1

That is because this line

$result = $conn->query($sql);

That is not what you want, you need to fetch the result like this

if ($result->num_rows > 0)
while($guest = $result->fetch_assoc()) {
{?>
    <div class="posts-container">
        <div class="post-header">
            <div class="user-details">
                <div class="user-name"><?php echo $guest["firstName"] ?></div>
                <div class="user-email"></div>
            </div>
            <div class="time"></div>
        </div>
        <div class="post-message">
            <h3></h3>
            <p></p>
        </div>
    </div>
<?php } ?>

3 Comments

the last error is gone but i now have a new error Illegal string offset 'firstName'
it fixes the error but i don't get anything from the database but it works with a other solutions but thanks
You're welcome, if my answer helped you with your question, I would appreciate it if you upvote my answer and choose it as the best answer as you wish.
-1

You can try something like this

<?php
//Array to be shown
$guests = [
        ['firstName' => 'Alex', 'email' => '[email protected]'] ,
        ['firstName' => 'Jhon', 'email' => '[email protected]']
];
?>
<div class="posts-container">
    <div class="post-header">
        <div class="user-details">
        <!-- foreach alternate syntax -->
        <?foreach ($guests as $guest):?>
            <div class="user-name"><?=$guest['firstName']?></div>
            <div class="user-email"><?=$guest['email']?></div>
        <!-- foreach alternate syntax -->
        <?endforeach;?>
    </div>
    <div class="time"></div>
</div>
<div class="post-message">
    <h3></h3>
    <p></p>
</div>

1 Comment

Please share more details such that others can learn from it

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.