EniGma
EniGma

Reputation: 2464

Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5

Here is my php code,i searched about the solution but even after adding var_dump($request); i got the same Notice<Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5>.

index.php

<?php 
$rd = dirname(__FILE__);
$request[]='';
var_dump($request);
if ($request[1] == '')
  {
        $request[1] = 'header';
    include($rd.'/php_includes/'.$request[1].'.php');
  }


if ($request[0] == '')
  {
    $request[0] = 'index';
    include($rd.'/php_includes/'.$request[0].'.php');
  }

?>

Could you please Help me out with this?

Upvotes: 0

Views: 23943

Answers (3)

Aryan Arka Ghosh
Aryan Arka Ghosh

Reputation: 1

<?php
    include("db.php");
    $query = "SELECT * FROM `tbl_user` ORDER BY id DESC";
    $statement = mysqli_query($connect,$query);
    // if(count($data)>0){
        while($row = mysqli_fetch_assoc($statement))
        {
            echo $row[1];
        }   

Upvotes: 0

Arrok
Arrok

Reputation: 116

You are getting the error because youare calling $request[1] when it is not set. If you want $request to be declared as an empty array do it like:

$request = array();

if you want to check if it is set or empty

if (!isset($request[1]) || empty($request[1]))
{
    //your code here in case of not existing or being empty
}

$request[] = ''; // adds a value to the next key - they are autogenerated from 0 as int 0 1 2 3 4 5 6 and so on, if you don't declare them otherwise

Upvotes: 1

Nick Coons
Nick Coons

Reputation: 3692

Initially, $request doesn't exist. You add one element with $request=[]'', so now you have $request[0] set. Shortly thereafter, you reference $request[1] without it being first defined. There is no $request[1], which is why you're getting this notice.

Your line that checks to see if it has a value, and throws the notice because it's not set, is this one:

if ($request[1] == '')

If you want to check to see if it's empty without throwing a notice, use this:

if (empty($request[1]))

This will return TRUE if $request[1] is not set, is set to NULL, empty, or 0; so it should accomplish what you're trying to do without throwing a notice.

Upvotes: 3

Related Questions