Home > database >  I am getting this error Warning: Trying to access array offset on value of type null
I am getting this error Warning: Trying to access array offset on value of type null

Time:01-10

I am getting this error

Warning: Trying to access array offset on value of type null in E:\xampp\htdocs\word-meaning-learn\word-ajax-insert.php on line 15 Inserted the meaning data!

What's wrong with the code on the line? > if($row['bangla_mean'] == $bangla_mean)

<?php
include "config.php";
$bangla_mean = $_POST["bangla_mean"];
$english_mean = $_POST["english_mean"];
$example_mean = $_POST["example_mean"];
$synonym_mean = $_POST["synonym_mean"];

if(isset($bangla_mean)){
  $stmt = $conn->prepare("SELECT bangla_mean FROM wordmeanings_table WHERE bangla_mean=?");
$stmt->bind_param("s",$bangla_mean);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);

if($row['bangla_mean'] == $bangla_mean){
  $response = "This Bangla meaning already exist!";
  }
 else{
  $stmt = $conn->prepare("INSERT INTO wordmeanings_table (bangla_mean, english_mean, example_mean, synonym_mean) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss",$bangla_mean,$english_mean,$example_mean,$synonym_mean);
      if($stmt->execute()){
        $response = "Inserted the meaning data!";
      }
      else{
        $response = "Something went wrong!";
      }
  }
}

echo $response;
        exit;
?>


CodePudding user response:

The error message means that $row variable is null, but you're trying to treat it as an array. Null is a legal returning value of fetch_array() method when no rows found. If row found, method returns an associative array for requested "bangla_mean" from POST.

if (is_array($row)) {
    $response = "This Bangla meaning already exist!";
} else {
    // ...
}

Or another option to check $row is not a null:

if ($row !== null) {
    // ...
}

CodePudding user response:

You query is empty you try to get the result of "?", it's not logic.. try to do that instead:

if(isset($bangla_mean)){
  $stmt = $conn->prepare("SELECT bangla_mean FROM wordmeanings_table WHERE bangla_mean='$bangla_mean'");
$stmt->bind_param("s",$bangla_mean);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);
  •  Tags:  
  • php
  • Related