Empty Record Errors

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 24, 2023   (RSS)

By rez - February 20, 2023 - edited: February 20, 2023

Actually, the code generator doesn't use if statements. My memory is bad, but I thought so!

OK, why am I getting this error everywhere for empty records then?

Notice: Trying to access array offset on value of type null in /home2/website/public_html/index.php on line 149

  list($about_usRecords, $about_usMetaData) = getRecords(array(
    'tableName'   => 'about_us',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $about_usRecord = @$about_usRecords[0]; // get first record



    <section class="theme-b">
      <div class="grid-container">		
        <div class="grid-x grid-padding-x text-center align-center-middle">
          <div class="cell medium-8 padding-vertical-2">
              <h2><?php echo $about_usRecord['title']; ?></h2>
              <?php echo $about_usRecord['content']; ?>
          </div>
        </div>
      </div>
    </section>

?

By Dave - February 24, 2023

Hi Rez, 

If there's no record then the $about_usRecord will be undefined, so you'll get errors.  

Normally, the code generator will display a die statement to check if the record isn't defined.

$about_usRecord = @$about_usRecords[0]; // get first record
if (!$about_usRecord ) { dieWith404("Record not found!"); } // show error message if no record found

So that's one option, or you can wrap the code you don't want displayed in an if block like you did, or you can add @ in front of the variables you don't want to display errors when they're undefined.

Hope that helps!  Let me know any questions.

Dave Edis - Senior Developer
interactivetools.com