0

I really have searched for the answer for this and not found a specific solution...

I've recently moved a site to a different server and the PHP.ini must be set to log errors that were not previously logged. Realise that I could suppress this error, but would rather remove the cause of it.

Database query:

$SQL = "SELECT * FROM table WHERE id > 0";
$retid = $conn->query($SQL) or die($mysqli->error.__LINE__);
while($row = $retid->fetch_assoc()) {
$var1 = $row["var1"]);
$var2 = $row["var2"]);}

and the error in the error_log file:

"Undefined index: var2 in /home/domain/public_html/directory/file.php on line 129", with line 129 being:

$var2 = $row["var2"]);}

I have read, and I get, that this error can be caused by undefined $_GET, $_POST or $_SESSION variables, but if it is thrown up each time an SQL query returns an empty value, is there a simple way to avoid building up these errors, bearing in mind that I am retrieving a lot of values with each query, so without repeating isset() arguments each time?

Thank you

1
  • In short, no. You either need to handle the error, or live with it Commented Mar 3, 2019 at 22:38

2 Answers 2

1

I know you said you don't want to use isset() but I think that may be what is needed since it looks like you're attempting to access an array element which does not exist.

while($row = $retid->fetch_assoc()) {
   $var1 = isset($row["var1"]) ? $row["var1"] : "";
   $var2 = isset($row["var2"]) ? $row["var2"] : "";
}

I also noticed that you had what looks to be an extra closing parenthesis after both closing square brackets.

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

Comments

0

Do this

$var2 = $row['var2'];

Be sure that var2 is a correct name of a column on your Database table And I don't know why you bring close bracket ) at the end of the array

Comments

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.